Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: How do I inject ENUM in Spring configuration with generics?

I have a class like this :

public class CheckSetFilter<E extends Enum<E>> 
{
   public CheckSetFilter(CheckSetManager<E> pCheckSetManager, E pCheckSetId)
}

I have this enum :

public enum StubCheckId
{
   STUBCHECK1, STUBCHECK2
}

I try to create such an object with Spring :

<bean id="checkSetFilter" class="com.iba.icomp.core.checks.CheckSetFilter">
   <constructor-arg ref="checkSetManager"/>
   <constructor-arg value="STUBCHECK1"/>
</bean>

It complains, it cannot convert from String to Enum. I guess this is because of the generic. It cannot know the type of enum to create. I also tried to give it a type hint, but no luck.

like image 435
PeeWee2201 Avatar asked Apr 23 '13 16:04

PeeWee2201


1 Answers

All you really have to do is to add a value tag inside the constructor-arg tag.

<bean id="checkSetFilter" class="com.iba.icomp.core.checks.CheckSetFilter">
    <constructor-arg ref="checkSetManager"/>
    <constructor-arg>
        <value type="your.package.StubCheckId">STUBCHECK1</value>
    </constructor-arg>
</bean>
like image 187
maba Avatar answered Sep 22 '22 04:09

maba