Today, in the process of using the spring cache's cacheable annotations, a cacheable comment was encountered, and the reason for the problem was that the spring cacheable annotations were implemented based on spring AOP. However, the class internal methods are not intercepted by spring AOP when they call each other, so the cacheable annotations of the called methods are invalidated and are hereby recorded.
Problem recurrence
@ServicePublicClassuserserviceimpl{@OverridePublic UserDetail(Long ID) {Verifying informationif (id = =null | | id = =0) {return apiresult.instance (). Fail (userresultenum.user_id _null); } User user = this.selectbyid (ID); if (user = null) {return Apiresult.instance (). Fail (userresultenum.user_null); } return user;} @Override @Cacheable (value = "user", Condition = "#id! = null", key = "' User '. Concat (#id. toString ())") public User selectById (Serializable id) {return super.selectbyid ( ID); }}
The above code when using This.selectbyid cacheable annotations are invalid, the workaround is as follows:
- Write a tool class Springcontextutil implement Applicationcontextaware interface
PublicClassSpringcontextutilImplementsApplicationcontextaware {PrivateStatic ApplicationContext ApplicationContext; @Override public void setapplicationcontext (ApplicationContext ApplicationContext) {springcontextutil.applicationcontext = ApplicationContext;} public static ApplicationContext getapplicationcontext () {return ApplicationContext; } public static Object getbean (class var1) throws BeansException {return Applicationcontext.getbean (VAR1);}}
- Inject the applicationcontext into the springcontextutil when spring boot boots
public class AuthServiceApplication { public static void main(String[] args) { SpringContextUtil springContextUtil = new SpringContextUtil(); ApplicationContext applicationContext = SpringApplication.run(AuthServiceApplication.class, args); springContextUtil.setApplicationContext(applicationContext); }}
- Used in the Userserviceimpl method
@ServicePublicClassuserserviceimpl{@OverridePublic UserDetail(Long ID) {Verifying informationif (id = =null | | id = =0) {Return Apiresult.instance (). Fail (userresultenum.user_id_null); }//injects the current bean so that the calling internal method is also SPRINGAOP intercept iuserservice userservice = (iuserservice) Springcontextutil.getbean (this.getclass ()); User user = Userservice.selectbyid (ID); if (user = null) {return Apiresult.instance (). Fail (userresultenum.user_null); } return user;} @Override @Cacheable (value = "user", Condition = "#id! = null", key = "' User '. Concat (#id. toString ())") public User selectById (Serializable id) {return super.selectbyid ( ID); }}
This solves the problem that the bean internal method call is not blocked by spring AOP
Spring's bean internal method call cannot use AOP facets (cacheable annotations expire)