| # 创建普通类 |
| public class User { |
| |
| public void add() { |
| System.out.println("add......."); |
| } |
| |
| } |
| |
| # 创建增强类 |
| public class UserProxy { |
| |
| public void before() {//前置通知 |
| System.out.println("before......"); |
| } |
| |
| } |
| |
| # 开启注解扫描 |
| <?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.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> |
| |
| <context:component-scan base-package="com.ychen.spring.aopanno"></context:component-scan> |
| |
| </beans> |

-
增强类上面添加注解 @Aspect
-
配置文件中开启生成代理对象
| <aop:aspectj-autoproxy></aop:aspectj-autoproxy> |
| @Before(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| @Test |
| public void testAopAnno() { |
| ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); |
| User user = context.getBean("user", User.class); |
| user.add(); |
| } |
| |
| # 测试 |
| before......... |
| add....... |
| |
| @Component |
| public class User { |
| |
| public void add() { |
| System.out.println("add......."); |
| } |
| |
| } |
| |
| |
| @Component |
| @Aspect |
| public class UserProxy { |
| |
| |
| @Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void before() { |
| System.out.println("before........."); |
| } |
| |
| |
| @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterReturning() { |
| System.out.println("afterReturning........."); |
| } |
| |
| |
| @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void after() { |
| System.out.println("after........."); |
| } |
| |
| |
| @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterThrowing() { |
| System.out.println("afterThrowing........."); |
| } |
| |
| |
| @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
| System.out.println("环绕之前........."); |
| |
| proceedingJoinPoint.proceed(); |
| System.out.println("环绕之后........."); |
| } |
| |
| } |
| |
| |
| <?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.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> |
| <!-- 开启注解扫描 --> |
| <context:component-scan base-package="com.atguigu.spring5.aopanno"></context:component-scan> |
| |
| <!-- 开启Aspect生成代理对象--> |
| <aop:aspectj-autoproxy></aop:aspectj-autoproxy> |
| </beans> |
| |
| |
| @Test |
| public void testAopAnno() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| User user = context.getBean("user", User.class); |
| user.add(); |
| } |
| |
| |
| 环绕之前......... |
| before......... |
| add....... |
| 环绕之后......... |
| after......... |
| afterReturning......... |
| @Component |
| public class User { |
| |
| public void add() { |
| |
| int i = 10/0; |
| System.out.println("add......."); |
| } |
| |
| } |
| |
| # 控制台 |
| 环绕之前......... |
| before......... |
| after......... |
| afterThrowing......... |
| |
| java.lang.ArithmeticException: / by zero |
| |
| at com.atguigu.spring5.aopanno.User.add(User.java:11) |
| |
| @Component |
| public class User { |
| |
| public void add() { |
| System.out.println("add......."); |
| } |
| |
| } |
| |
| |
| @Component |
| @Aspect |
| public class UserProxy { |
| |
| |
| @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void pointdemo() { |
| |
| } |
| |
| |
| |
| @Before(value = "pointdemo()") |
| public void before() { |
| System.out.println("before........."); |
| } |
| |
| |
| @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterReturning() { |
| System.out.println("afterReturning........."); |
| } |
| |
| |
| @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void after() { |
| System.out.println("after........."); |
| } |
| |
| |
| @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterThrowing() { |
| System.out.println("afterThrowing........."); |
| } |
| |
| |
| @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
| System.out.println("环绕之前........."); |
| |
| proceedingJoinPoint.proceed(); |
| System.out.println("环绕之后........."); |
| } |
| |
| } |
| |
| |
| <?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.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> |
| <!-- 开启注解扫描 --> |
| <context:component-scan base-package="com.atguigu.spring5.aopanno"></context:component-scan> |
| |
| <!-- 开启Aspect生成代理对象--> |
| <aop:aspectj-autoproxy></aop:aspectj-autoproxy> |
| </beans> |
| |
| |
| @Test |
| public void testAopAnno() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| User user = context.getBean("user", User.class); |
| user.add(); |
| } |
| |
| |
| 环绕之前......... |
| before......... |
| add....... |
| 环绕之后......... |
| after......... |
| afterReturning......... |
- 有多个增强类对同一个普通类进行增强的时候,可以定义增强类的优先级
| |
| |
| @Component |
| public class User { |
| |
| public void add() { |
| System.out.println("add......."); |
| } |
| |
| } |
| |
| |
| @Component |
| @Aspect |
| @Order(3) |
| public class UserProxy { |
| |
| |
| @Pointcut(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void pointdemo() { |
| |
| } |
| |
| |
| |
| @Before(value = "pointdemo()") |
| public void before() { |
| System.out.println("before........."); |
| } |
| |
| |
| @AfterReturning(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterReturning() { |
| System.out.println("afterReturning........."); |
| } |
| |
| |
| @After(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void after() { |
| System.out.println("after........."); |
| } |
| |
| |
| @AfterThrowing(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterThrowing() { |
| System.out.println("afterThrowing........."); |
| } |
| |
| |
| @Around(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
| System.out.println("环绕之前........."); |
| |
| proceedingJoinPoint.proceed(); |
| System.out.println("环绕之后........."); |
| } |
| |
| } |
| |
| |
| @Component |
| @Aspect |
| @Order(1) |
| public class PersonProxy { |
| |
| |
| @Before(value = "execution(* com.atguigu.spring5.aopanno.User.add(..))") |
| public void afterReturning() { |
| System.out.println("Person Before........."); |
| } |
| |
| } |
| |
| |
| <?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.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> |
| <!-- 开启注解扫描 --> |
| <context:component-scan base-package="com.atguigu.spring5.aopanno"></context:component-scan> |
| |
| <!-- 开启Aspect生成代理对象--> |
| <aop:aspectj-autoproxy></aop:aspectj-autoproxy> |
| </beans> |
| |
| |
| @Test |
| public void testAopAnno() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean1.xml"); |
| User user = context.getBean("user", User.class); |
| user.add(); |
| } |
| |
| |
| Person Before......... |
| 环绕之前......... |
| before......... |
| add....... |
| 环绕之后......... |
| after......... |
| afterReturning......... |
| |
| Process finished with exit code 0 |
| # 普通类 |
| public class Book { |
| |
| public void buy() { |
| System.out.println("buy............."); |
| } |
| |
| } |
| |
| # 增强类 |
| public class BookProxy { |
| |
| public void before() { |
| System.out.println("before........."); |
| } |
| |
| } |
| |
| # bean.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.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> |
| |
| <bean id="book" class="com.atguigu.spring5.aopxml.Book"></bean> |
| <bean id="bookProxy" class="com.atguigu.spring5.aopxml.BookProxy"></bean> |
| |
| |
| <aop:config> |
| |
| <aop:pointcut id="p" expression="execution(* com.atguigu.spring5.aopxml.Book.buy(..))"/> |
| |
| <aop:aspect ref="bookProxy"> |
| |
| <aop:before method="before" pointcut-ref="p"/> |
| </aop:aspect> |
| </aop:config> |
| </beans> |
| |
| # 测试方法 |
| @Test |
| public void testAopXml() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean2.xml"); |
| Book book = context.getBean("book", Book.class); |
| book.buy(); |
| } |
| |
| # 控制台 |
| before......... |
| buy............. |
| |
| Process finished with exit code 0 |
- 纯注解开发方式

| # 普通类 |
| @Component |
| public class User { |
| |
| public void add() { |
| |
| |
| System.out.println("add......."); |
| } |
| |
| } |
| |
| # 增强类1 |
| @Component |
| @Aspect |
| @Order(3) |
| public class UserProxy { |
| |
| |
| @Pointcut(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| public void pointdemo() { |
| |
| } |
| |
| |
| |
| @Before(value = "pointdemo()") |
| public void before() { |
| System.out.println("before........."); |
| } |
| |
| |
| @AfterReturning(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| public void afterReturning() { |
| System.out.println("afterReturning........."); |
| } |
| |
| |
| @After(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| public void after() { |
| System.out.println("after........."); |
| } |
| |
| |
| @AfterThrowing(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| public void afterThrowing() { |
| System.out.println("afterThrowing........."); |
| } |
| |
| |
| @Around(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { |
| System.out.println("环绕之前........."); |
| |
| proceedingJoinPoint.proceed(); |
| System.out.println("环绕之后........."); |
| } |
| |
| } |
| |
| # 增强类2 |
| @Component |
| @Aspect |
| @Order(1) |
| public class PersonProxy { |
| |
| |
| @Before(value = "execution(* com.ychen.spring.aopanno.User.add(..))") |
| public void afterReturning() { |
| System.out.println("Person Before........."); |
| } |
| |
| } |
| |
| # 配置类 |
| @Configuration |
| @ComponentScan(basePackages = {"com.ychen"}) |
| @EnableAspectJAutoProxy(proxyTargetClass = true) |
| public class ConfigAop { |
| } |
| |
| # 测试方法 |
| public class Test1 { |
| |
| @Test |
| public void testService2() { |
| ApplicationContext context |
| = new AnnotationConfigApplicationContext(ConfigAop.class); |
| User user = context.getBean("user", User.class); |
| user.add(); |
| } |
| |
| } |
| |
| # 控制台 |
| Person Before......... |
| 环绕之前......... |
| before......... |
| add....... |
| 环绕之后......... |
| after......... |
| afterReturning......... |
| |
| Process finished with exit code 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术