Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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?

like image 295
xelurg Avatar asked Feb 05 '09 16:02

xelurg


People also ask

Can we assign value to enum?

Enum ValuesYou can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

Can an enum be a bean?

Enums are not beans because they have no default constructor (hence CDI does not know how to construct them,) and there is no standard way to resolve which enumerated value should be injected by default (Unless there is only one value, but this is still not supported due to the lack of default constructor.)


1 Answers

Have you tried just "TYPE1"? I suppose Spring uses reflection to determine the type of "type" anyway, so the fully qualified name is redundant. Spring generally doesn't subscribe to redundancy!

like image 186
krosenvold Avatar answered Sep 26 '22 17:09

krosenvold