Use aspect or advisor in Spring to implement interception and simulate cache implementation

Source: Internet
Author: User

AOP is a technology that separates general logic from specific services. It can make up for the problem of insufficient horizontal code reuse of OO and implement separation of concerns (SoC) Well ). Cache is a common technology used to improve system performance. It adopts a space-for-time strategy. Cache is unrelated to specific services. If we design a cache framework, it should be pluggable and non-invasive to System Business Code, which is in line with the applicable scenarios of AOP. Our project uses the Ehcache cache framework as the underlying support. We use the Spring framework's AOP method to intercept and cache the results returned by the time-consuming method. Now we use spring aop to simulate this implementation.

First, let's assume that we have a well-written, time-consuming class.

Package net. aty. dao; public class DaoImpl {public String query (int id) {// simulate time-consuming database query operations try {System. out. println ("query begin... "); Thread. sleep (100 * id); System. out. println ("query over... ");} catch (InterruptedException e) {e. printStackTrace ();} return "result:" + id * 10 ;}}

Below is the cache implementation class we have compiled

package net.aty.cache;import java.util.HashMap;import java.util.Map;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.Signature;public class CacheQueryResult{private Map
 
   buffer = new HashMap
  
   ();public Object around(ProceedingJoinPoint point) throws Throwable{String key = uniqueKey(point);Object returnValue = buffer.get(key);if(returnValue != null){return returnValue;}Object object = point.proceed();buffer.put(key, object);return object;}private String uniqueKey(ProceedingJoinPoint point){Object target = point.getTarget();Signature signature = point.getSignature();String methodSignature = signature.toString();String key = target.hashCode() + methodSignature;return key;}}
  
 

Now we configure spring. xml to block the target object from the cache aspect.

 
 
  
  
  
  
 

The test class is as follows:

package net.aty;import net.aty.dao.DaoImpl;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestMain{private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");public static void main(String[] args){for (int i = 0; i < 3; i++){testDao();}}public static void testDao(){DaoImpl dao = (DaoImpl) context.getBean("dao");long begin = System.currentTimeMillis();String result = dao.query(2);long end = System.currentTimeMillis();System.out.println(result + ",cost time:" + (end - begin));}}

The execution result shows that the data obtained from the cache is directly obtained in the next two calls, which achieves the cache simulation effect.

Finally, the structure of the project and the jar of spring3.1.2 dependent are attached.

Vcq9o6y077W9wcu3vbeowLm92LXE0Ke5 + kernel + DNrLXE0Ke5 + 6GjPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java;"> package net. aty. cache; import org. aopalliance. intercept. methodInterceptor; import org. aopalliance. intercept. methodInvocation; public class CacheMethodInterceptor implements MethodInterceptor {@ Overridepublic Object invoke (MethodInvocation invocation) throws Throwable {System. out. println ("begin MethodInterceptor"); Object result = invocation. proceed (); System. out. println ("end MethodInterceptor"); return result ;}}
The xml configuration is as follows:

 


Related Article

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.