Spring 4 autowiring of generic classes works in test, doesn't work when deployed to server

♀尐吖头ヾ 提交于 2019-12-25 06:46:50

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/31771265/spring-4-autowiring-of-generic-classes-works-in-test-doesnt-work-when-deployed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!