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 ?

Answers


Check out the <util:constant> tag in Spring. It will require you to add the schema to your xml definition. So you would wind up with the following:

<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.


Need Your Help

Chrome user agent stylesheet overwriting my site style

html ruby-on-rails css google-chrome web

I have the following CSS that styles the link on one of my pages:

NSDateFormatter Bug : incorrect string to date conversion

objective-c nsdateformatter utc

I have an a string in UTC which i want to convert to NSDate. Somehow Im getting 30 minutes error