五种通知注解
- 前置通知: @Before
- 后置通知: @AfterReturing
- 环绕通知: @Around
- 最终通知: @After
- 异常通知: @AfterThrowing
注意1: 使用环绕通知时,默认是不执行切点方法的,需要手动引用,详情见例子中
注意2: spring4的执行顺序和spring5的执行顺序不同
spring4的执行顺序: @Around1,@Before,原方法,@Around2,@After,@AfterReturning
spring5.3.16的执行顺序: @Around前置,@Before,原方法,@AfterReturning,@After,@Around后置
注意: 出现异常时,后置通知,和后置环绕通知不输出,如果有异常通知,在此处输出异常通知
1. 导入jar包
* 先引入Spring框架开发的基本开发包
* 再引入Spring框架的AOP的开发包
* spring的传统AOP的开发的包
* spring-aop-4.2.4.RELEASE.jar
* com.springsource.org.aopalliance-1.0.0.jar
* aspectJ的开发包
* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
* spring-aspects-4.2.4.RELEASE.jar
2. 创建目标对象
package com.daben.service.impl;
import org.springframework.stereotype.Service;
import com.daben.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
@Override
public void save() {
System.out.println("保存...");
}
@Override
public void update() {
System.out.println("更新...");
}
}
3. 创建切面类
package com.daben.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspectAnnontation {
// 自定应切入点(即取代通知时繁琐的切入点表达式,引用自定义的切入点时直接类名.切入点方法)
@Pointcut(value="execution(public * com.daben.service.impl.UserServiceImpl.save())")
public void fn(){}
// 前置通知(切入点的表达式)
@Before("MyAspectAnnontation.fn()")
public void beforeNotice() {
System.out.println("前置通知...");
}
// 后置通知
@AfterReturning("MyAspectAnnontation.fn()")
public void afterNotice() {
System.out.println("后置通知");
}
// 环绕通知
@Around("MyAspectAnnontation.fn()")
public void aroundNotice(ProceedingJoinPoint joinPoint) {
System.out.println("环绕通知111");
try {
joinPoint.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("环绕通知222");
}
// 最终通知
@After("MyAspectAnnontation.fn()")
public void lastNotice() {
System.out.println("最终通知");
}
}
创建配置文件
在src下创建applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.daben"/>
<!-- 开启自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
SpringBoot中使用注解开发
- 导入AOP相关依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
- 遇上述的切面类一样,编写完切面类就可以使用了.
声明:
有一些博文是看的黑马程序员视频,然后跟着老师做的笔记
Spring是跟子路老师学的
特此感谢,写这些文章的目的是为了自己方便查阅