如何理解面向切面AOP

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包->

[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. @Component   
  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方法



@Component  用于将此类注入到spring中,交由spring进行实例化,方法调用.

@Pointcut("execution (* test.spring.service.impl.PersonServiceBean.*(..))")   当执行("")中的方法时,获取到切点,此时此类的某些方法会被调用.

@Before 前置执行

@After 后置执行

@Around 环绕(同时执行)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山隐的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值