目录
- <dependency>
- <groupId>org.aspectjgroupId>
- <artifactId>aspectjweaverartifactId>
- <version>1.9.19version>
- dependency>

运行测试类
- package com.example.Test;
-
-
- import com.example.Service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestMyAOP {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- UserService userService = applicationContext.getBean(UserService.class);
- userService.show1();
- }
- }
运行结果如下

| 通知类型 | 描述 |
|---|---|
| Before | 在目标方法执行之前执行的通知,用于进行准备工作或检查 |
| After | 在目标方法执行之后执行的通知,用于进行清理工作或资源释放,最终都会执行 |
| AfterReturning | 在目标方法正常执行并返回结果后执行的通知,用于处理返回值或记录结果,目标方法异常时,不再执行 |
| AfterThrowing | 在目标方法抛出异常后执行的通知,用于捕获和处理异常 |
| Around | 在目标方法执行前后都可以执行的通知,用于包裹目标方法的执行过程、控制和干预,目标方法异常时,环绕后方法不再执行 |
| StaticInitialization | 静态初始化代码块的通知,当类被加载时执行 |
| Initialization | 对象初始化代码块的通知,当对象被创建时执行 |
| FieldGet | 字段读取操作的通知,当访问字段时执行 |
| FieldSet | 字段赋值操作的通知,当修改字段值时执行 |
| MethodExecution | 方法执行的通知,包括Before、After、AfterReturning和AfterThrowing等通知的集合 |
- package com.example.advice;
-
-
- import org.aspectj.lang.ProceedingJoinPoint;
-
- // 自定义增强类,内部提供增强方法
- public class MyAdvice {
- // todo 前置通知
- public void beforeAdvice() {
- System.out.println("前置通知");
-
- }
-
- // todo 后置通知
- public void afterAdvice() {
- System.out.println("后置通知");
-
- }
-
- // todo 环绕通知
- public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
- // 前置通知
- System.out.println("环绕通知:前置通知");
- Object res = proceedingJoinPoint.proceed(); // 执行目标方法
- System.out.println("环绕通知中目标方法执行了");
- // 后置通知
- System.out.println("环绕通知:后置通知");
- return res;
- }
-
- // todo 异常通知
- public void afterThrowingAdvice() {
- System.out.println("异常抛出通知...出现异常才会执行");
- }
-
- // todo 最终通知
- public void endAdvice() {
- System.out.println("最终通知....怎么样都会通知");
- }
-
- }
xml配置文件
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <bean id="userService" class="com.example.Service.ServiceImpl.UserServiceImpl"/>
-
- <bean id="myAdvice" class="com.example.advice.MyAdvice"/>
-
- <aop:config>
-
- <aop:pointcut id="MyPointCut"
- expression="execution(void com.example.Service.ServiceImpl.UserServiceImpl.*(..))"/>
-
-
- <aop:aspect ref="myAdvice">
-
- <aop:before method="beforeAdvice" pointcut-ref="MyPointCut"/>
-
- <aop:after-returning method="afterAdvice" pointcut-ref="MyPointCut"/>
-
- <aop:around method="around" pointcut-ref="MyPointCut">aop:around>
-
- <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="MyPointCut"/>
-
- <aop:after method="endAdvice" pointcut-ref="MyPointCut"/>
- aop:aspect>
- aop:config>
- beans>
测试代码
- package com.example.Test;
-
-
- import com.example.Service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestMyAOP {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- UserService userService = applicationContext.getBean(UserService.class);
- userService.show1();
- }
- }
运行结果如下

上述的运行结果也多多少少体现了各种通知类型的通知顺序, 具体可以参照文章:AOP进阶-通知顺序-CSDN博客,同时由于目标方法没有运行错误,所以,异常通知类无法通知,造出异常后:

- package com.example.advice;
-
- import org.springframework.aop.AfterReturningAdvice;
- import org.springframework.aop.MethodBeforeAdvice;
-
- import java.lang.reflect.Method;
-
- public class MyAdvice2 implements MethodBeforeAdvice, AfterReturningAdvice {
- @Override
- public void before(Method method, Object[] objects, Object o) throws Throwable {
- System.out.println("前置通知~~~~~~");
- }
-
- @Override
- public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
- System.out.println("后置通知~~~~~~");
- }
-
-
- }
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
-
- <bean id="userService" class="com.example.Service.ServiceImpl.UserServiceImpl"/>
-
- <bean id="myAdvice2" class="com.example.advice.MyAdvice2"/>
- <aop:config>
- <aop:pointcut id="MyPointCut" expression="execution(* com.example.Service.ServiceImpl.UserServiceImpl.*(..))"/>
- <aop:advisor advice-ref="myAdvice2" pointcut-ref="MyPointCut">aop:advisor>
- aop:config>
-
- beans>
测试类
- package com.example.Test;
-
-
- import com.example.Service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestMyAOP {
- public static void main(String[] args) {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
- UserService userService = applicationContext.getBean(UserService.class);
- userService.show1();
- }
- }
运行结果如下



| 代理技术 | 使用条件 | 配置方式 |
| JDK动态代理技术 | 目标类有接口,是基于接口动态生成实现类的代理对象 | 目标类有接口的情况下,默认方式 |
| Cglib动态代理技术 | 目标类无接口且不能用final修饰,是基于被代理对象动态生成子对象为代理对象 | 目标了无接口时,默认使用该方式;目标类有接口时,手动配置 |
