Open spring-projects-issues opened 6 years ago
Juergen Hoeller commented
This is strongly related to self-injection: That self-injected early bean reference gets flagged as such in AbstractAutoProxyCreator
, expecting to see a pre-proxied instance in postProcessAfterInitialization
which it doesn't have to add any interceptors to anymore. While this is an accurate assumption in most cases, it can lead to accidental exposure of an incomplete proxy in self-injection scenarios.
Juergen Hoeller commented
It's generally a good idea to break such a self-reference initialization cycle with an @Lazy
reference (or corresponding ObjectFactory
/Provider
declaration), in this case for the meWithAProxy
reference. We'll see what we can do to make it work without that as well but probably only in 5.x.
Is there anyone working on this?
Hi, I would like to contribute on it, pls assign to me, thanks
When I tested, it works fine : Code:
@Service @CacheConfig public class Util {
Util util;
@Autowired
ApplicationContext appCont;
@Cacheable(value = "pi", key = "#inp")
public int calPi(int inp) {
System.out.println("inp "+inp);
return inp * 3;
}
@PostConstruct
public void init() {
util = appCont.getBean(Util.class);
System.out.println("POST construct "+util.getClass());
}
@Async
public void methodOne() {
System.out.println("asyc");
util.evictCache();
}
@CacheEvict(value= "pi", allEntries = true)
public void evictCache() {
System.out.println("cacheevict");
}
public void methodTwo() {
util.evictCache();
System.out.println(this.getClass());
}
}
@SpringBootApplication @Configuration @EnableCaching public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(DemoApplication.class);
Util util = context.getBean(Util.class);
util.calPi(2);
util.calPi(2);
util.methodOne();
util.calPi(2);
util.evictCache();
util.calPi(2);
util.methodTwo();
util.calPi(2);
} }
Output: POST construct class com.amit.test.demo.Util$$SpringCGLIB$$0 inp 2 asyc cacheevict inp 2 cacheevict inp 2 cacheevict class com.amit.test.demo.Util inp 2
Here we get "inp 2" printed 4 times which is expected
Henri Tremblay opened SPR-15915 and commented
If I use
@Async
and@CacheEvict
on the same class, calling the method with@CacheEvict
directly won't evict anymore.Let's say I have the following class.
And I then use it like that:
For some reason, the cache interceptor is not there. It seems that the Advisors with the cache are replaced by the async ones but I don't know why.
Affects: 4.3.10
Reference URL: https://stackoverflow.com/questions/45938279/cache-not-refreshing-when-being-called-from-a-asynchrounous-function-in-spring/45963494#45963494
Issue Links:
18488 Scheduled method is not invoked via proxy