Spring 4 autowiring of generic classes works in te

2019-09-18 14:07发布

I have generic classes which autowires dependencies based on their generic type like this:

public abstract class GenericRestService<C extends AbstractTenantEntity> extends RestResource<C> {

    protected final Logger log;
    @Autowired(required = false)
    protected GenericResourceService<C> service;
    @Autowired(required = true)
    protected JpaRepository<C, Long> repo;

 // Now use service if one with specified generic type C is found, otherwise use repo

In my spring tests everything works: if no GenericResourceService<C> with concrete generic type C is defned nothing is injected into field service and I happily resort to using repo which gets autowired.

However when I run my application in real environment there is always injected some implementation of GenericResourceService<C> regardless of whether it's generic type matches type required by dependency.

2条回答
可以哭但决不认输i
2楼-- · 2019-09-18 14:35

You might getting multiple bean definition found. Maybe spring 4 does not support dynamic type generics autowiring.

查看更多
3楼-- · 2019-09-18 14:44

It was caused by JDK proxy vs. cglib proxy clash. One was used in deploymen't the other in test.

查看更多
登录 后发表回答