SpringCache @ Cacheable calls methods in the same class, which causes the cache to fail and the solution is as follows,

Source: Internet
Author: User

SpringCache @ Cacheable calls methods in the same class, which causes the cache to fail and the solution is as follows,

Because the project needs to use SpringCache for a bit of caching, but it has never been used before (in fact, it has never been heard of) SpringCache, so you must first learn it.

Find an article on the Internet. It is better to learn it first. The address is:

Https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/

I would like to thank the original bloggers for their selfless dedication ~~ Thank you ~~

 

In one breath, it is easy to understand and not difficult. At the same time, my personal habit is to Copy the code, Paste to Idea for debugging and running, and submit the code to Github. Therefore, for SpringCache, a Repository is created on Github:

Https://github.com/cyhbyw/springCache

 

The Demo is quickly completed. A few days later, the corresponding code is also used in the project. Everything looks so smooth ......

One day, when I tested it myself, I found that the cache method did not take effect, and the method was still called, so I went to the DB to retrieve data instead of from the cache.

So, look for RootCause.

Go back to the article and write it clearly, for example:

Obviously, if MethodA calls MethodB in the same class, the @ Cacheable comment on MethodB will become invalid!

And the solution, people also said:

That is: SpringAOP cannot be solved. We need to use AspectJ to solve it!

 

Then, the next question is: How can I switch from SpringAOP to AspectJ?

I searched a lot on the Internet (I can't remember how I found the results step by step). The final solution is as follows:

Step 1:

SpringCache uses the default mode = proxy and needs to be set to mode = aspectj, as shown in figure 2 below.

Previous:

After modification:

So what does this mode mean? View the XSD file as follows:

Is the annotation Bean using SpringAOP or AspectJ for proxy? The AspectJ proxy needs to add a spring-aspects.jar to the ClassPath while enabling load-time weaving or compile-time weaving ). At the same time, annotations can only act on classes. The interface will not take effect.

Therefore, changing mode = proxy to mode = aspectj is natural.

Step 2:

Complete the actions required for the translation.

Step 2: Add spring-aspects.jar dependencies to POM

Step 2: Add a plug-in POM to enable the compile-time weaving function. The content of this plug-in is as follows:

 

Test again. @ Cacheable in MethodB has taken effect!

 

Finally, I would like to dedicate my Github address again:

Https://github.com/cyhbyw/springCache

Project name: ibmSpringCache

Submission record for resolving this problem:

 

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.