PeeWee2201
PeeWee2201

Reputation: 1534

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.

Upvotes: 14

Views: 27000

Answers (1)

maba
maba

Reputation: 48075

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>

Upvotes: 29

Related Questions