问题
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