实际增强的逻辑部分称为通知(增强)
1、前置通知
2、后置通知
3、环绕通知
4、异常通知
5、最终通知
切入点
实际被真正增强的方法,称为切入点
AOP切面操作
动作:把通知应用到切入点过程
Spring框架一般都是基于AspectJ实现AOP操作
AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spirng框架一起使用,进行AOP操作
引入AOP依赖


切入点表达式
execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )
举例1:对com.leavescai7.dao.BookDao类里面的add进行增强
execution(* com.leavescai7.dao.BookDao.add(..))
实例操作:
1、创建一个User (被增强的类)
1 package com.leavescai7.aopanno; 2 3 import org.springframework.stereotype.Component; 4 5 //被增强的类 6 @Component 7 public class User { 8 public void add(){ 9 System.out.println("add..."); 10 } 11 }
2、创建增强类(编写增强逻辑)
1 package com.leavescai7.aopanno; 2 3 import org.aspectj.lang.annotation.Aspect; 4 import org.aspectj.lang.annotation.Before; 5 import org.springframework.stereotype.Component; 6 7 //增强的类 8 @Component 9 @Aspect //生成代理类 10 public class UserProxy { 11 @Before(value = "execution(* com.leavescai7.aopanno.User.add(..))") //把以下方法(功能)插入到原来的方法之前执行,这就是不该变源代码,通过代理类增加功能 12 public void before(){ 13 System.out.println("before..."); 14 } 15 }
3、进行通知的配置
①开启注解扫描 ②开启生成代理对象
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 9 <!--开启注解扫描--> 10 <context:component-scan base-package="com.leavescai7.aopanno"></context:component-scan> 11 <!-- 开启Aspect生成代理对象--> 12 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 13 </beans>
4、创建一个Test
package com.leavescai7.Test; import com.leavescai7.aopanno.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test public void testAopAnno(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); User user = context.getBean("user", User.class); user.add(); } }

配置不同类型的通知
在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
1 //增强的类 2 @Component 3 @Aspect //生成代理对象 4 public class UserProxy { 5 //前置通知 6 //@Before注解表示作为前置通知 7 @Before(value = "execution(* com.leavecai7.aopanno.User.add(..))") 8 public void before() { 9 System.out.println("before..."); 10 } 11 12 //后置通知(返回通知) 13 @AfterReturning(value = "execution(*com.leavecai7.aopanno.User.add(..))") 14 public void afterReturning() { 15 System.out.println("afterReturning..."); 16 } 17 18 //最终通知 19 @After(value = "execution(* com.leavecai7.aopanno.User.add(..))") 20 public void after() { 21 System.out.println("after........."); 22 } 23 24 //异常通知 25 @AfterThrowing(value = "execution(* com.leavecai7.aopanno.User.add(..))") 26 public void afterThrowing() { 27 System.out.println("afterThrowing........."); 28 } 29 30 //环绕通知 31 @Around(value = "execution(* com.leavecai7.aopanno.User.add(..))") 32 public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 33 System.out.println("环绕之前........."); 34 //被增强的方法执行 35 proceedingJoinPoint.proceed(); 36 System.out.println("环绕之后........."); 37 } 38 }
浙公网安备 33010602011771号