把动态代理配置到IOC 称之为AOP 切面 切开方法,植入想要的东西(代码)
配置AOP
四种通知类型
-
前置通知 调用方法之前去执行的代码 org.springframework.aop.MethodBeforeAdvice
-
后置通知 调用方法之后去执行的代码 org.springframework.aop.AfterReturningAdvice
-
环绕通知 对方法进行拦截之后,前后都可植入代码 org.aopalliance.intercept.MethodInterceptor
-
异常通知 在调用方法的时候如果发生异常去执行的代码 org.springframework.aop.ThrowsAdvice
1、新建拦截器

2、配置applicationContext.xml

3、调用,手动解析xml文件
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Imi mi=(Imi) context.getBean("proxyFactory");
mi.productNote2();
以上使用较为繁琐
下面使用扫包+注解的方式
1、导包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
2、新建一个类,用于处理;在类名上,需要加 @Aspect 的注解

3、创建处理方法

-
@Before 前置通知
-
@After 后置通知
-
@AfterReturning 后置通知,只有方法返回值时才触发
-
@AfterThrowing 异常通知
-
@Around 环绕通知
4、applicationContext.xml

5、使用

四种通知执行顺序


本文详细介绍Spring AOP中四种主要的通知类型:前置通知、后置通知、环绕通知及异常通知,并展示了如何通过注解方式实现这些通知,简化了AOP配置过程。
6573

被折叠的 条评论
为什么被折叠?



