使用Spring的注解方式实现面向切面技术AOP

原文:http://blog.csdn.net/a352193394/article/details/7345860

或 http://blog.csdn.net/lindonglian/article/details/47058919

 

 

AOP涉及到的一些概念:

Aspect(切面):横切性关注点的抽象即为切面,与类相似,只是两者的关注度不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象。
Joinpoint(连接点):所谓连接点指那些被拦截的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点(实际上Joinpoint还可以是field或类构造器)。
Pointcut(切入点):所谓切入点是指我们要对那些Joinpoint进行拦截的定义。
Advice(通知):所谓通知是指拦截到Joinpoint之后所要做的事情。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知。
Target(目标对象):代理的目标对象。
Weave(织入):指将Aspect应用到Target对象并导致Proxy对象创建的过程。
Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field。

Spring提供了基于xml配置和基于注解两种方式进行AOP开发。

使用AOP在beans.xml中也有一些自己的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                    xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     <aop:aspectj-autoproxy />
     <bean id="myInterceptor" class="test.spring.aop.MyInterceptor"></bean>
     <bean id="personService" class="test.spring.service.impl.PersonServiceBean"></bean>
</beans> 

导入一些必须的jar包->

 aspectjrt.jar,aspectjweaver.jar,cglib-nodep.jar.

[java] view plain copy
 
 print?
  1. package test.spring.service;  
  2.   
  3. public interface PersonService {  
  4.   
  5.     public void save(String name);  
  6.   
  7.     public String getResult();  
  8.   
  9. }  

 

[java] view plain copy
 
 print?
  1. package test.spring.service.impl;  
  2.   
  3. import test.spring.service.PersonService;  
  4.   
  5. //代理对象实现目标对象所有接口  
  6. public class PersonServiceBean implements PersonService {  
  7.   
  8.     public PersonServiceBean() {  
  9.   
  10.     }  
  11.   
  12.     @Override  
  13.     public void save(String name) {  
  14.         System.out.println("save()->>" + name);  
  15.         throw new RuntimeException(">>----自定义异常----<<");  
  16.     }  
  17.   
  18.     @Override  
  19.     public String getResult() {  
  20.         return "getResult()==>>返回结果";  
  21.     }  
  22.   
  23. }  

切面

 

[java] view plain copy
 
 print?
  1. package test.spring.aop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11. import org.springframework.stereotype.Component;  
  12.   
  13. @Aspect  
  14. public class MyInterceptor {  
  15.     // 切入点  
  16.     @Pointcut("execution (* test.spring.service.impl.PersonServiceBean.*(..))")  
  17.     private void anyMethod() {  
  18.     }// 声明一个切入点  
  19.         // 无参数的切入点  
  20.         // @Before("anyMethod()")  
  21.   
  22.     // 定义有参数的切入点,参数名称需相同。这里对拦截到的方法只有只有一个String参数的方法才有用  
  23.     @Before("anyMethod() && args(userName)")  
  24.     public void doAccessCheck(String userName) {  
  25.         System.out.println("前置通知-->>" + userName);  
  26.     }  
  27.   
  28.     // 获取有返回结果的通知  
  29.     @AfterReturning(pointcut = "anyMethod()", returning = "result")  
  30.     public void doAfterReturning(String result) {  
  31.         System.out.println("后置通知-->>" + result);  
  32.     }  
  33.   
  34.     @After("anyMethod()")  
  35.     public void doAfter() {  
  36.         System.out.println("最终通知");  
  37.     }  
  38.   
  39.     // 调用的方法出现异常才会调用这个方法  
  40.     @AfterThrowing(pointcut = "anyMethod()", throwing = "e")  
  41.     public void doAfterThrowing(Exception e) {  
  42.         System.out.println("异常通知-->" + e);  
  43.     }  
  44.   
  45.     @Around("anyMethod()")  
  46.     public Object doAround(ProceedingJoinPoint pJoinPoint) throws Throwable {  
  47.         System.out.println("环绕通知");  
  48.         // 这里如果pJoinPoint.proceed()不执行,后面拦截到的方法都不会执行,非常适用于权限管理  
  49.         Object result = pJoinPoint.proceed();  
  50.         System.out.println("退出");  
  51.         return result;  
  52.     }  
  53.   
  54. }  

定义完切面,要交给Spring管理才会有效果。交给Spring有两种方式,一种是在beans.xml中定义bean,另一种是通过@Component扫描

[java] view plain copy
 
 print?
  1. package test.spring.junit;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.support.AbstractApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import test.spring.service.PersonService;  
  8.   
  9. public class AOPTest3 {  
  10.   
  11.     @Test  
  12.     public void test() {  
  13.         AbstractApplicationContext aContext = //  
  14.         new ClassPathXmlApplicationContext("beans.xml");  
  15.         PersonService pService = (PersonService) aContext  
  16.                 .getBean("personService");  
  17.         pService.save("LinDL");  
  18.         // pService.getResult();  
  19.         aContext.close();  
  20.     }  
  21.   
  22. }  

测试save方法

 



测试getResult方法

posted on 2017-09-12 13:45  杭州糊涂虫  阅读(192)  评论(0)    收藏  举报

导航