spring aop的简单使用方法详解
2019-10-07 10:28
1291 查看
AOP:【动态代理】
指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式;
1、导入aop模块;Spring AOP:(spring-aspects)
2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行结束、方法出现异常,xxx)
3、定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行;
通知方法:
- 前置通知(@Before):logStart:在目标方法(div)运行之前运行
- 后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
- 返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
- 异常通知(@AfterThrowing):logException:在目标方法(div)出现异常以后运行
- 环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
4、给切面类的目标方法标注何时何地运行(通知注解);
5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
[7]、给配置类中加 @EnableAspectJAutoProxy 【开启基于注解的aop模式】
在Spring中很多的 @EnableXXX;
三步:
1)、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个是切面类(@Aspect)
2)、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式)
3)、开启基于注解的aop模式;@EnableAspectJAutoProxy
package com.opk.bean; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import java.util.Arrays; /* * @Aspect: 告诉Spring当前类是一个切面类 * wmy 13:03 2019/9/5 * @Param * @return **/ @Aspect public class LogAspects { //抽取公共的切入点表达式 //1、本类引用 //2、其他的切面引用 @Pointcut("execution(public * com.opk.bean.*.*(..))") public void pointCut(){}; //@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入) @Before("pointCut()") public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(joinPoint.getSignature().getName()+":运行。。。@Before,参数列表是:"+ Arrays.asList(args)); } @After("com.opk.bean.LogAspects.pointCut()") public void logEnd(JoinPoint joinPoint){ System.out.println(""+joinPoint.getSignature().getName()+"结束。。。@After"); } //JoinPoint一定要出现在参数的第一位 @AfterReturning(value ="pointCut()",returning = "result") public void logReturn(JoinPoint joinPoint,Object result){ System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:运行结果:{"+result+"}"); } @AfterThrowing(value ="pointCut()",throwing = "ex") public void logException(JoinPoint joinPoint,Exception ex){ System.out.println(""+joinPoint.getSignature().getName()+"异常。。。异常信息:{"+ex+"}"); } }
import org.springframework.stereotype.Component; @Component public class MathCalculator { public int dev(int i,int j) { System.out.println("MathCalculator......"); return i/j; } }
@EnableAspectJAutoProxy @Configuration public class AOPConfig { //业务逻辑类加入容器中 @Bean("calculator") public MathCalculator calculator(){ return new MathCalculator(); } //切面类加入到容器中 @Bean public LogAspects logAspects(){ return new LogAspects(); } }
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AOPConfig.class); ctx.refresh(); MathCalculator bean = (MathCalculator)ctx.getBean("calculator"); bean.dev(10,3); }
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- 关于spring切面使用<aop:aspectj-autoproxy/>报错的解决方法
- Spring AOP详解及通过XML配置实现的实现方法
- 一个简单的Spring的AOP例子传统方法
- 使用SpringAop 验证方法参数是否合法
- Spring AOP-->面向切面编程简单理解和简单使用
- Spring boot中PropertySource注解的使用方法详解
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- 从头认识Spring-3.2 简单的AOP日志实现-需要记录方法的运行时间
- 为什么SpringAOP使用JDK动态代理时好像没有代理Object.[equals()、hashCode()、toString()]这三个方法
- springboot 中使用 AOP 打印 类名、方法名、方法参数名称和方法参数值
- 详解Spring Boot中MyBatis的使用方法
- SpringAOP过滤目标方法的简单实现
- 详解使用Spring Boot的AOP处理自定义注解
- 使用AOP,在spring中实现简单的性能测试
- 使用SpringAop 验证方法参数是否合法
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况)
- Spring简单获得实体类的实例, 使用ApplicationContext()方法的几点注意事项
- 使用Spring的AOP实现接口方法执行时间记录
- 微信小程序 textarea 详解及简单使用方法