Spring不同版本的AOP
2024-09-03 06:31:56
1、Spring4、SpringBoot1
1.1 代码实现
public interface Calculator { int div(int a,int b);
}
@Component
public class CalculatorImpl implements Calculator{
@Override
public int div(int a, int b) {
int result = a/b;
System.out.println("计算结果为:"+result);
return result;
}
}
@Aspect
@Component
public class CalculatorAspect {
@Before(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))")
public void beforeNotify(){
System.out.println("@Before...前置通知");
}
@After(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))")
public void afterNotify(){
System.out.println("@After...后置通知");
}
@AfterReturning(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))")
public void afterReturningNotify(){
System.out.println("@AfterReturning...返回通知");
}
@AfterThrowing(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))")
public void afterThrowingNotify(){
System.out.println("@AfterThrowing...异常通知");
}
@Around(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))")
public Object aroundNotify(ProceedingJoinPoint joinPoint) throws Throwable{
Object res = null;
System.out.println("@Around...环绕通知之前A");
res = joinPoint.proceed();
System.out.println("@Around...环绕通知之后B");
return res;
}
}
1.2 测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringBoot1ApplicationTests {
@Autowired
private Calculator calculator; @Test
public void contextLoads() {
System.out.println("Spring版本:"+ SpringVersion.getVersion()+"\t"+"SpringBoot版本:"+ SpringBootVersion.getVersion());
calculator.div(5,2);
} }
2、Spring5、SpringBoot2
3、Spring6、SpringBoot3
最新文章
- sql with as union all
- segues的类型
- java9-9 匿名内部类
- Vim的文件加密
- POJ 1316
- PHP移动互联网的发展票据(6)——MySQL召回数据库基础架构[1]
- Docker基本架构
- 使用swagger管理接口
- webpack打包优化
- Uart串口
- 人生苦短之HTTP协议及Requests库的方法
- MyBatis是如何解决Sql注入的
- C 语言中的 switch 语句 case 后面是否需要加大括号
- C:\\MFC控件大小随窗体大小而改变
- vue项目引入FastClick组件解决IOS系统下h5页面中的按钮点击延迟,连续点击无反应的问题
- Axure8.1.0.3372 注册码
- JS面向对象(封装,继承)
- Intent 对象在 Android 开发中的应用
- [小技巧]Linux的一些信息获取
- mysql 约束条件 外键 forigen key 介绍