简体   繁体   中英

Use a generic class with type parameters in xmlbean

I search a solution for use with bean a generic class like this:

public class CompositeMapper<T extends Model, U extends Command, C extends Context> implements Mapper<T, U, C>
{ 
    protected List<Mapper<T, U, C>> mappers;

    /* ... */
}

I would use a bean like this:

<bean id="beanMapper" class="com.test.CompositeMapper">
    <property name="mappers">
        <list value-type="com.test.Mapper">
            <bean class="com.test.Mapper1"/>
            <bean class="com.test.Mapper2"/>
            <bean class="com.test.Mapper2"/>
        </list>
    </property>
</bean>

Streangly, this bean works. But I can use anything that is enter in the generic case, so not specialized.

But I don't find a solution for typed my generic object com.test.Mapper<com.test.MyModel, com.test.MyCommand, com.test.MyContext> , it not possible in value-type. I not found other solution than to create an empty class for replace com.test.CompositeMapper in my bean. For example :

public class MyCompositeMapper extends CompositeMapper<MyModel, MyCommand, MyContext>
{
    public MyCompositeMapper()
    {
        super();
    }
}

But I would avoid creating this class, is it possible?

No, value-type is used for type conversion so note that if com.test.Mapper.class.isAssingnableFrom(com.test.Mapper1.class) returns true , it will be accepted.

However, you could use Java Configuration classes to enforce the generic type checks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM