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,520 points
10,050 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,840 points

edited Mar 4, 2022 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,770 points
1,080 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
2,133 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,770 points
2,013 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,840 points
986 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
888 views
0 votes
2 answers

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

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered Nov 26, 2023 in Career Counselling by heukrzewyb
2,259 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,460 points
925 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP