Spring's bean internal method call cannot use AOP facets (cacheable annotations expire)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.