commit related in spring framework https://github.com/spring-projects/spring-framework/commit/5aefcc802ef05abc51bbfbeb4a78b3032ff9eee3
initialization is set to a later stage from afterPropertiesSet () to afterSingletonsInstantiated ()
In short : This prevents caching from being used when using @PostConstruct.
Longer version : This prevents use when you
create service B using @Cacheable on method B
create service A with a call to @ PostConstructB.methodB
@Component public class ServiceA{ @Autowired private ServiceB serviceB; @PostConstruct public void init() { List<String> list = serviceB.loadSomething(); }
This causes org.springframework.cache.interceptor.CacheAspectSupport not to initialize or cache the result.
protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) { // check whether aspect is enabled // to cope with cases where the AJ is pulled in automatically if (this.initialized) { //>>>>>>>>>>>> NOT Being called Class<?> targetClass = getTargetClass(target); Collection<CacheOperation> operations = getCacheOperationSource().getCacheOperations(method, targetClass); if (!CollectionUtils.isEmpty(operations)) { return execute(invoker, new CacheOperationContexts(operations, method, args, target, targetClass)); } } //>>>>>>>>>>>> Being called return invoker.invoke(); }
My workaround is to manually call the initialization method:
@Configuration public class SomeConfigClass{ @Inject private CacheInterceptor cacheInterceptor; @PostConstruct public void init() { cacheInterceptor.afterSingletonsInstantiated(); }
This, of course, fixes my problem, but does it have side effects, others that are simply called 2 times (1 manual and 1 for the intended purpose).
My question is: "Is this a safe workaround to do as the initial committer seems to have had a problem using the afterPropertiesSet () method"
java spring-cache postconstruct
TimothyBrake
source share