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, 2020 in Career Counselling by kartik
• 37,510 points
5,822 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 

Hope this is helpfull!!

Get your Spring Framework Certification today and become certified.

Thanks!

answered May 22, 2020 by Niroj
• 82,680 points

edited Mar 4 by Sarfaraz

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
• 65,850 points
299 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
1,233 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 13, 2019 in Career Counselling by Gitika
• 65,850 points
415 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, 2020 in Career Counselling by Niroj
• 82,680 points
477 views
0 votes
2 answers

How can I start my career as a Java developer?

Java is one of the most popular ...READ MORE

answered Dec 25, 2020 in Career Counselling by IT Examples
236 views
0 votes
1 answer

What are the skills required to become a Java Full stack developer?

Hello @kartik, If you want to become a ...READ MORE

answered May 20, 2020 in Career Counselling by Niroj
• 82,680 points
998 views
0 votes
2 answers

How to get job offer in google?

Hi@yahaya, You can get a job offer on ...READ MORE

answered Oct 30, 2020 in Career Counselling by MD
• 95,340 points
285 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP