How to inject parameters in enum constructor using Spring?

spring value enum
enum with multiple values in java
java enum constructor
java enum lookup by field
java enum int value
java enum with string values
java enum valueof
validate enum values spring boot

I have an enum like this:

public enum SomeEnum {
    ONE (new MyClass()),
    TWO (new MyClass());

    private final MyClass instance;

    private SomeEnum(MyClass instance) {
        this.instance = instance;
    }
}

How can I pass MyClass instance to the enum constructor from Spring context? Is it even possible?

I need it because I pass some parameters from config (.properties file) into MyClass instance while I create it. Now I'm doing it in xml-file with beans, maybe there is another way?

You cannot do this.

In this official Java tutorial on Enum Types, it states

Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

Since an Enum is supposed to be a constant set of constants, it doesn't make sense to be able to create new ones, so the constructors are not available, even through reflection.

Inject Dependency Value Into Enum, properties wont be possible with Spring - you would need to guarantee that Spring has loaded and parsed the configuration, then try to inject it  In spring config xml, we need to inform to the spring IOC container about constructor injection by using < constructor – arg /> In spring bean class, if both constructor and setter injection applied for same property then constructor injection will be overridden by setter injection, because constructor injection will happen at the object

Even when we talk in context of Spring , i think that is also not possible.

You cannot instantiate enums because they have a static nature. So I think that Spring IoC can't create enums as well.

please have a look at Spring IoC chapter.

Attaching Values to Java Enum, I just announced the new Learn Spring course, focused on the In this tutorial, we'll use the enum's features as a Java class to attach the values we want. an enum, we can pass constructor arguments in the declaration list. We can inject the dependency by constructor. The <constructor-arg> subelement of <bean> is used for constructor injection. Here we are going to inject. primitive and String-based values. Dependent object (contained object) Collection values etc.

What you can do however, is leverage MyClass within the enum to encapsulate some 'constant' behaviour. You can take this as far as you like (not that it's necessarily a great idea...) for example you might use some static factory class to load property values based on those constant names passed in.

public enum SomeEnum {
    ONE ("propname1"),
    TWO ("propname2");

    private final MyClass instance;

    private SomeEnum(String str) {
        this.instance = MyClassFactory.newInstance(str);
    }

    public Object doSomething(int value) {
        return instance.doSomething(value);
    }
}

Java enum - enum constructor, method, inheritance, Learn enum constructors, methods, inheritance, EnumMap and EnumSet etc. We should use enum when we know all possible values of a variable at Adding a concrete method in enum is similar to add same method in any other class. the command line argument to view the generated class out of the enum please? Spring doesn't know which particular instance to be injected in that case. If the name of parameter is same as bean provider method (the factory method) name then the dependency is resolved by name. The bean provider method can provide a different name than the method name by using @Bean(name = ) , the injection point method's parameter name should match in that case as well.

Enums as Request Parameters in Spring Boot Rest, But while using Spring controllers to expose REST endpoints, these enums are not matched How to Convert String in Request Parameters to Enum questions.add(question); return questions; } @InitBinder public void  ‘@Autowired’ and ‘@Inject’ can be used interchangeable to inject Spring beans. However the ‘@Resource’ annotation uses the ‘CommonAnnotationBeanPostProcessor’ to inject dependencies. Even though they use different post processor classes they all behave nearly identically. Below is a summary of their execution paths.

How to @Inject Enum values into CDI beans, recently asked how to Inject a Java Enum into a bean constructor using CDI. Parameter 1 of [BackedAnnotatedConstructor] @Inject public  How to Inject property values into a static property or method of an enum or normal class using Spring? Tag: java , spring , configuration , dependency-injection , static I am in serious trouble due to a situation.

Spring @Value, A field or constructor parameter can be injected with enum value using @Value annotation. Find a sample enum file. Spring essentially “injects” the argument into your bean. This is referred to as constructor injection. In this video we will learn Constructor injection with a example Check out our website

Comments
  • I think you can instead declare MyClass instances as beans in the container and transform SomeEnum to qualifier. Generally it's not a good idea to mix Java singletons with Spring singleton beans.