SpringAOP注解开发以及测试
话不多少现在的需求就是我们模拟一个简单的除以方法说起:
public class MathCalculator { public int div(int i,int j){ System.out.println("MathCalculator...div..."); return i/j; } }
AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式;
没错就是这么简单那么我们要干什么呢?说的就是AOP嘛我们给它做个切面,也就是跟踪到它算到哪儿了,遇到异常返回一类的,换句话说就是日志嘛我们由此引入AOP上代码+注释喽需求说明:
定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候将日志进行打印(方法之前、方法运行结束、方法出现异常,xxx)
* 3、定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行;
代码目录:
好我们上代码这个就是我们的切面类:
@Aspect public class LogAspects { @Pointcut("execution(public int cn.test.aop.MathCalculator.*(..))")//切入点我们都抽取到这里面 public void pointcut(){} //前置通知 @Before("pointcut()")//@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入) public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(""+joinPoint.getSignature().getName()+"除法运行@Before.....参数{"+Arrays.asList(args)+"}"); } //后置通知 @After("pointcut()") public void logEnd(JoinPoint joinPoint){ System.out.println(""+joinPoint.getSignature().getName()+"除法结束@After{}"); } //正常返回执行 @AfterReturning(value = "pointcut()",returning = "result") public void logReturn(JoinPoint joinPoint,Object result){ System.out.println("除法正常运行"+joinPoint.getSignature().getName()+"@AfterReturning{"+result+"}"); } //异常信息 @AfterThrowing(value = "pointcut()",throwing = "exception") public void lodExpetion(JoinPoint joinPoint,Exception exception){ System.out.println("除法异常"+joinPoint.getSignature().getName()+"[email protected]{"+exception+"}"); } }
顺便附上各个切面的用法:
通知方法:
* 前置通知(@Before):logStart:在目标方法(div)运行之前运行
* 后置通知(@After):logEnd:在目标方法(div)运行结束之后运行(无论方法正常结束还是异常结束)
* 返回通知(@AfterReturning):logReturn:在目标方法(div)正常返回之后运行
* 异常通知(@AfterThrowing):logException:在目标方法(div)出现异常以后运行
@Pointcut是用来拦截里面参数说有的方法 其他的我就不做说明了我们看到我们把何时何地切入,怎么切入都告诉这个类了然后呢?
@EnableAspectJAutoProxy @Configuration public class MyConfigofAOP { //将切面类和业务逻辑类(目标方法所在类)都加入到容器中; @Bean public MathCalculator mathCalculator(){ return new MathCalculator(); } //切面类 @Bean public LogAspects logAspects(){ return new LogAspects(); } }
这样我们们都把它交给了IOC去处理啦
重要说明:
*4、给切面类的目标方法标注何时何地运行(通知注解);
* 5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
* 6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
* [7]、给配置类中加 @EnableAspectJAutoProxy 【开启基于注解的aop模式】
* 在Spring中很多的 @EnableXXX;
//JoinPoint一定要出现在参数表的第一位 关于和玩意儿是什么请右拐百度
好了我们改切的也切了,该配置的也配置了那就启动实施了:
@Test public void test01() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfigofAOP.class); //不要自己创建对象,要用ioc里面的哦 /*MathCalculator mathCalculator = new MathCalculator(); mathCalculator.div(1, 1);*/ MathCalculator bean = applicationContext.getBean(MathCalculator.class); bean.div(1,1); applicationContext.close(); }
输出正常:我们同时也做了异常处理那就试一下异常:
最后贴出要的pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>ok这个例子我们了解了AOP的注解配置的形式那么下一篇我们追一追源码