How to inject a Jakarta enums in a Spring application context?


How to inject a Jakarta enums in a Spring application context?



I have a class which constructor takes a Jakarta enums. I'm trying to find how I can easily inject it via an Spring XML aplicationContext.

For example :

The enum :

public class MyEnum extends org.apache.commons.lang.enums.Enum {     public static final MyEnum MY_FIRST_VALUE = new MyEnum("MyFirstValue");     public static final MyEnum MY_SECOND_VALUE = new MyEnum("MySecondValue");      public static MyEnum getEnum(String name) {         return (MyEnum) getEnum(MyEnum.class, name);     }     [...other standard enum methods] } 

The class in which to inject :

public class MyService {     private final MyEnum status;     public MyService(MyEnum status) {         this.status = status;     } } 

The application context :

<bean id="myService" class="MyService">     <constructor-arg index="0" value="MyFirstValue" /> </bean> 

Of course, with this I have a no matching editors or conversion strategy found error. Is there an easy integration between Spring and the Jakarta enums ? Or should I write my own PropertyEditor ?




What is the simplest solution to integrate 2 apps within a Tomcat server?

1:



netbeans and Apache Derby Embedded + spring
Check out the <util:constant> tag in Spring.


Does Spring-MVC annotation based bean validation support for collection-based property ? (Spring-MVC)
It will require you to add the schema to your xml definition.


Spring 2 Hibernate Annotation
So you would wind up with the following:.
Two approaches of task distribution in a multi tier application
<bean id="myService" class="MyService">   <constructor-arg index="0">     <util:constant static-field="MyEnum.MY_FIRST_VALUE"/>   </constructor-arg> </bean> 
The definition and usage of the tag (including the XSD def) is found here..
What's the most AGILE APPROACH to validate Spring-MVC command (speeds up validation implementation on server side) ? (Spring-MVC)


ConstraintViolationException VS DataIntegrityViolationException


Testing an AspectJ Aspect

2:


I found a solution, but it is very verbose (far too much to my taste) :.
<bean id="myService" class="MyService">     <constructor-arg index="0">         <bean class="MyEnum" factory-method="getEnum">             <constructor-arg value="MyFirstValue" />         </bean>     </constructor-arg> </bean> 


3:


I voted-up Spencer's answer but found that this will work as well.

In this example, you just need to specify the index, type, and name in the constructor-arg..
<bean id="myService" class="MyService"> <constructor-arg index="0" type="com.package.MyEnum" value="MyFirstValue" /> </bean> 
I based this off of an error message I received:.
(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities).



88 out of 100 based on 48 user ratings 1198 reviews

*