转自:
http://www.java265.com/JavaFramework/Spring/202206/3612.html
下文笔者将通过示例的方式讲述Spring注解(AOP)的简介说明,如下所示:-
- package com.java265.aop;
- public class MathCalculator {
- public int div(int i, int j){
- System.out.println("MathCalculator...div...");
- return i / j;
- }
- }
- 切面类
- package com.java265.aop;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.*;
- import java.util.Arrays;
- //日志切面类
- @Aspect
- public class LogAspects {
- //收取公共的切入点表达式 1.本类引用:直接写方法名+()2.其他类引用:全名
- @Pointcut("execution(public int com.java265.aop.MathCalculator.div(int, int))")
- public void pointCut(){}
- //@Before在目标方法前切入;切入点表达式(指定在哪个方法切入);JointPoint 一定要在参数第一位
- @Before("pointCut()")
- public void logStart(JoinPoint joinPoint){
- Object[] args = joinPoint.getArgs();
- System.out.println(joinPoint.getSignature().getName() + "运行了...参数列表是:{" + Arrays.asList(args)+ "}");
- }
- @After("pointCut()")
- public void logEnd(JoinPoint joinPoint){
- System.out.println(joinPoint.getSignature().getName() + "结束了...");
- }
- @AfterReturning(value = "pointCut()", returning = "result")
- public void logReturn(JoinPoint joinPoint, Object result){
- System.out.println(joinPoint.getSignature().getName() + "正常返回...运行结果:{" + result + "}");
- }
- //外部类引用切入点表达式
- @AfterThrowing(value = "com.java265.aop.LogAspects.pointCut()", throwing = "e")
- public void logException(JoinPoint joinPoint, Exception e){
- System.out.println(joinPoint.getSignature().getName() + "异常...异常信息:{" + e + "}");
- }
- }
-
- 配置类:
- package com.java265.config;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.EnableAspectJAutoProxy;
- import com.java265.aop.LogAspects;
- import com.java265.aop.MathCalculator;
- /**
- * AOP:指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式;
- *
- * 1、 导入aop模块:Spring AOP
- * 2、 定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候将日志进行打印(方法之前,方法运行结束、出现异常等)
- * 3. 定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行
- * 通知方法:
- * 前置通知(@Before):logStart:在目标方法div运行之间运行
- * 后置通知(@After):logEnd:在目标方法div运行结束之后运行
- * 返回通知(@AfterReturning):logReturn:在目标方法div正常返回之后运行
- * 异常通知(@AfterThrowing):logException:在目标方法div出现异常以后运行
- * 环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
- * 4. 给切面类的目标方法标注何时运行(通知注解)
- * 5. 将切面类和业务逻辑类(目标方法所在类)都加入到IOC容器中
- * 6. 必须告诉Spring哪个类是切面类(给切面类加一个注解@Aspect)
- * 7. 给配置类中加@EnableAspectJAutoProxy开启基于注解的aop模式
- */
- @EnableAspectJAutoProxy
- @Configuration
- public class MainConfigOfAOP {
- //业务逻辑类加入容器中
- @Bean
- public MathCalculator mathCalculator(){
- return new MathCalculator();
- }
- //切面类加入到容器中
- @Bean
- public LogAspects logAspects(){
- return new LogAspects();
- }
- }
-
- 测试:
- @Test
- public void test01(){
- AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
- MathCalculator mathCalcullator = (MathCalculator) ac.getBean("mathCalculator");
- mathCalcullator.div(1,1);
- ac.close();
- }
-
复制代码- -----运行以上代码,将输出以下信息-----
- div运行了...参数列表是:{[1, 1]}
- MathCalculator...div...
- div结束了...
- div正常返回...运行结果:{1}
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |