Spring初学之annotation实现AOP前置通知和后置通知
2024-08-27 20:01:45
实现两个整数的加减乘除,并在每个计算前后打印出日志。
ArithmeticCalculator.java:
package spring.aop.impl; public interface ArithmeticCalculator { int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j); }
ArithmeticCalculatorImpl.java:
package spring.aop.impl; import org.springframework.stereotype.Component; @Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator { public int add(int i, int j) {
int result = i+j;
return result;
} public int sub(int i, int j) {
int result = i-j;
return result;
} public int mul(int i, int j) {
int result = i*j;
return result;
} public int div(int i, int j) {
int result = i/j;
return result;
} }
LoggingAspect.java:
package spring.aop.impl; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; //把这个类声明为一个切面:1.把该类放入到IOC容器中,2.再声明一个切面 @Aspect
@Component
public class LoggingAspect { //声明该方法是一个前置通知:在目标方法之前执行
@Before("execution( public int spring.aop.impl.*.*(int,int) )")
public void beforeMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins"+args);
} //后置通知:在目标方法执行后执行(无论是否发生异常),的通知。
//在后置通知中不能访问目标方法的执行结果
@After("execution( public int spring.aop.impl.*.*(int,int) )")
public void afterMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
} }
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"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring.aop.impl" /> <!-- 使AspectJ注解起作用,自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试:
package spring.aop.impl.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.aop.impl.ArithmeticCalculator; public class Main {
public static void main(String[] args) { //1.创建spring的IOC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从容器中获取bean的实例
ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
//3.使用bean
int result = arithmeticCalculator.add(5, 8);
System.out.println("result:"+result); result = arithmeticCalculator.div(5, 8);
System.out.println("result:"+result);
}
}
输出:
The method add begins[5, 8]
The method add ends
result:13
The method div begins[5, 8]
The method div ends
result:0
最新文章
- zookeeper清除事物日志
- C语言新文法
- [_CN] Eclipse精要与高级开发技术 note
- php设计模式 适配器模式
- MySQL线上执行大事务或锁表操作
- bjfu1235 两圆公共面积
- MyBatis之四:调用存储过程含分页、输入输出参数
- UIButton关于setFont方法过时的解决方法
- 【转】querystring传递中文出现乱码的问题
- 关于看似简单的eclipse中tomcat小猫图标消失的问题解决
- JavaScripts学习日记——ECMAscript
- css 简单 返回顶部 代码及注释说明
- 关于Android开发中导出jar包后的资源使用问题解决
- Vue2.0+Node.js+MongoDB全栈打造商城系统 免费下载
- 2017-10-5模拟赛T2 小Z爱排序(sorting.*)
- ATM开学测试(未完成)
- axure rp ----专业的快速原型设计工具
- logback.xml配置示例
- (一)SpringMVC
- 使用socket BPF/Linux内核工程导论——网络:Filter(LSF、BPF、eBPF)