How to assign bean's property an Enum value in Spring config file?

0 votes

I have a standalone enum type defined, something like this:

package my.pkg.types;

public enum MyEnumType {
    TYPE1,
    TYPE2
}

Now, I want to inject a value of that type into a bean property:

<bean name="someName" class="my.pkg.classes">
   <property name="type" value="my.pkg.types.MyEnumType.TYPE1" />
</bean>

and that didn't work 

How should I Inject an Enum into a spring bean?

May 22 in Career Counselling by kartik
• 11,890 points
76 views

1 answer to this question.

0 votes

Hello @kartik,

Use the value child element instead of the value attribute and specify the Enum class name:

<property name="residence">
    <value type="SocialSecurity$Residence">ALIEN</value>
</property>

The advantage of this approach over just writing value="ALIEN" is that it also works if Spring can't infer the actual type of the enum from the property 

Thank You!!

answered May 22 by Niroj
• 23,950 points

Related Questions In Career Counselling

0 votes
2 answers

How to ask a HR to reschedule an interview in a good MNC?

Hey, This kind of query has been asked by ...READ MORE

answered May 27, 2019 in Career Counselling by Gitika
• 29,690 points
66 views
+1 vote
3 answers

How to start career in BiG Data as fresher?

Hi, you can start your career in ...READ MORE

answered Jun 4, 2019 in Career Counselling by Shushma
500 views
0 votes
1 answer

Education after Bachelor Of Engineer for java developer

Hey, As you have mentioned that you are working ...READ MORE

answered Jun 12, 2019 in Career Counselling by Gitika
• 29,690 points
84 views
0 votes
1 answer

Which is the best site to learn Java online with certification?

Hii Kartik, Edureka- Which is a Banglore based organisation which ...READ MORE

answered May 14 in Career Counselling by Niroj
• 23,950 points
32 views
0 votes
1 answer

How can I start my career as a Java developer?

Hello @kartik, Getting into this career typically requires ...READ MORE

answered May 14 in Career Counselling by Niroj
• 23,950 points
18 views
0 votes
1 answer
0 votes
1 answer

I passed my B.com in 2013. What are career prospectives that I can go through to be employed ?

Hello Kushasan, Well, there are a lot of ...READ MORE

answered May 11 in Career Counselling by Niroj
• 23,950 points
30 views