📜 ⬆️ ⬇️

Spring and @Autowired for ENUM types. Optional

As it is known, in Spring it is impossible to make bins for enumerated types without “crutches” - this type has no constructor.
Caused by: org.springframework.beans.factory.BeanCreationException: Error created bean with name 'demoEnum0' defined in file [... \ DemoEnum0.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [ru.itbasis.demo.spring.enums.DemoEnum0]: No default constructor found; nested exception is java.lang.NoSuchMethodException: ru.itbasis.demo.spring.enums.DemoEnum0. <init> ()

( commit )

In this post I will try to get around this limitation.

Step 1. Replace the factory method


Create an EnumHandlerBeanFactoryPostProcessor class that implements the BeanFactoryPostProcessor interface.
 @Component public class EnumHandlerBeanFactoryPostProcessor implements BeanFactoryPostProcessor { private static final transient Logger LOG = LoggerFactory.getLogger(EnumHandlerBeanFactoryPostProcessor.class.getName()); @Override public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException { final String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { final BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDefinitionName); LOG.debug("beanDefinitionName: {}", beanDefinitionName); final String beanClassName = beanDefinition.getBeanClassName(); LOG.debug("beanClassName: {}", beanClassName); try { final Class<?> beanClass = Class.forName(beanClassName); if (beanClass.isEnum()) { LOG.trace("found ENUM class: {}", beanClass); LOG.trace("interfaces: {}", beanClass.getInterfaces()); beanDefinition.setFactoryMethodName("values"); } } catch (ClassNotFoundException e) { LOG.error(e.getMessage(), e); } } } } 

Here, in future bins, we look for all classes of type ENUM and replace the factory constructor for them with a call to the “values” method from the bean. Now the result when creating this bean is not an error, but an array of its constants.

( commit )
')
But this is not enough - Spring will not allow just to take, and even to slip the created bin, because you cannot get such a bin by its type.
“Well, yes, the disaster is the beginning,” said Ivan the Fool, but he went on to dig further.

Step 2. "Draft" work


“Auburn” our enum-type into the interface and in the tested class we change the type from enum-type to
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .

Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .

Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
 Set: 
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .
Set:
public interface IEnum { }

@Component public class SpringEnum { @Autowired(required = false) private Set<IEnum> fieldEnumSet; public Set<IEnum> getFieldEnum() { return fieldEnumSet; } }
@EnumAnnotation @Component
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Component public @interface EnumAnnotation { }
@EnumAnnotation public enum DemoEnum0 implements IEnum { VALUE_0, VALUE_1 }
( )

3 Spring Autowire
BeanPostProcessor , «» :
@Component public class EnumBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware { private ApplicationContext context; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }

" getEnums ", IEnum :
private Set<IEnum> getEnums() { final Map<String, Object> enumMap = context.getBeansWithAnnotation(EnumAnnotation.class); LOG.debug("enumMap.size: {}", enumMap.size()); Set<IEnum> result = new HashSet<IEnum>(); for (Object o : enumMap.values()) { if (o.getClass().isArray()) { final IEnum[] o1 = (IEnum[]) o; Collections.addAll(result, o1); } else { result.add((IEnum) o); } } LOG.debug("result: {}", result); return result; }

, , - , enum-, .
Disclamer: , IEnum .

" isAutowiredEnumSetField ", , :
@SuppressWarnings("unchecked") private boolean isAutowiredEnumSetField(Field field) { if (!AnnotatedElementUtils.isAnnotated(field, Autowired.class.getName())) { return false; } final Class<?> fieldType = field.getType(); if (!Set.class.isAssignableFrom(fieldType)) { return false; } final ParameterizedType type = (ParameterizedType) field.getGenericType(); final Type[] typeArguments = type.getActualTypeArguments(); final Class<? extends Type> aClass = (Class<? extends Type>) typeArguments[0]; return aClass.isAssignableFrom(IEnum.class); }

, , :
@Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { LOG.debug("bean: {}", bean); LOG.debug("beanName: {}", beanName); final Set<IEnum> enums = getEnums(); if (enums.size() < 1) { return bean; } final Class<?> beanClass = bean.getClass(); final Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (isAutowiredEnumSetField(field)) { LOG.trace("field inject values."); field.setAccessible(true); ReflectionUtils.setField(field, bean, enums); } } return bean; }

.
( )

UPD: enum .

Source: https://habr.com/ru/post/229955/


All Articles