I came across a case where an AOP proxy created using @Cacheable breaks dependency injection in Spring 3.1.1. Here is my scenario:
I have an interface and a class that implements this interface using @Cacheable with the implemented method.
Interface example:
public interface ImgService { public byte[] getImage(String name); }
Implementation Example:
public class ImgServiceImpl implements ImgService { @Cacheable(cacheName = "someCache") public byte[] getImage(String name){
I also need the JUnit testing classes - one that introduces the interface, and the other an implementation:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:META-INF/spring.xml" }) public class ImgServiceTest { @Inject private ImgService; }
and
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:META-INF/spring.xml" }) public class ImgServiceImplTest { @Inject private ImgServiceImpl; }
Injection for the interface works fine. However, when I receive an injection of the implementation in the second test class, I get the message "Injection of autwired dependencies failed". I managed to debug it, and it turned out that ClassUtils.isAssignableValue () incorrectly compares the desired type with the proxy class. It is called by default, ListableBeanFactory. Even stranger is that if I remove the @Cacheable annotation from the implemented method and add it to another protected / private method, dependency injection will stop again. Is this a mistake and what will be the correct approach to solving this situation?
source share