志在指尖
用双手敲打未来

spring aop运用,spring aop注解,Spring切面编程

一、第一步,引证依赖类,在Pom.xml加入依赖
<dependencies><dependency><groupId>org.springframeworkgroupId><artifactId>spring-contextartifactId><version>5.1.12.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-webmvcartifactId><version>5.1.12.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-txartifactId><version>5.1.12.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-aspectsartifactId><version>5.1.12.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-testartifactId><version>5.1.12.RELEASEversion><scope>testscope>dependency><dependency><groupId>junitgroupId><artifactId>junitartifactId><version>4.12version><scope>testscope>dependency>dependencies>
二、第二步:增加装备类
1、@Configuration:声明该类为装备类
2、@ComponentScan(“com.lqy.spring.aop”):扫描相应的类,纳入spring容器中管理
3、@EnableAspectJAutoProxy:启用注解办法的Aop模式
importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(“com.lqy.spring.aop”)
@EnableAspectJAutoProxypublicclassAopConfig{
}
三、第三步:自定义逻辑运算
importorg.springframework.stereotype.Component;/***Calculator类需要在spring容器才干运用aop
*运用:@Component,一起运用@ComponentScan注解扫描时,要扫描到该类
**/@ComponentpublicclassCalculator{publicintdivInteger(inta,intb){
System.out.println(“除法运算”);returna/b;
}publicdoublediv(doublea,doubleb){
System.out.println(“除法运算”);returna/b;
}publicdoubleadd(doublea,doubleb){
System.out.println(“加法运算”);returna+b;
}
}
四、第四步:运算逻辑类切面注入类
1、@Before:办法履行之前
2、@After:办法履行之后(不管会不会出现反常都会履行)
3、@AfterReturning:办法正常履行回来之后
4、@AfterThrowing:办法发作反常履行
importjava.util.Arrays;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.After;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;/***类需要在spring容器才干运用aop,而且添加切面类的注解:@Aspect
**/@Aspect
@ComponentpublicclassCalculatorAop{/***公共切点*/@Pointcut(“execution(*com.lqy.spring.aop.Calculator.*(..))”)publicvoidpointCut(){}/***办法履行之前*/@Before(value=”execution(*com.lqy.spring.aop.Calculator.*(..))”)publicvoidbefore(JoinPointjoinPoint){
System.out.println(“”);
System.out.println(“===============================================================”);
System.out.println(“before办法:{“+joinPoint.getSignature().getDeclaringTypeName()+”.”+joinPoint.getSignature().getName()+”}开端履行:”);
System.out.println(“办法参数是:{“+Arrays.asList(joinPoint.getArgs())+”}”);
}/***办法履行之后(不管会不会出现反常都会履行)
*pointCut():运用公共的切点表达式*/@After(“pointCut()”)publicvoidafter(JoinPointjoinPoint){
System.out.println(“after办法:{“+joinPoint.getSignature().getDeclaringTypeName()+”.”+joinPoint.getSignature().getName()+”}履行完毕。”);
}/***办法正常履行回来之后*/@AfterReturning(value=”pointCut()”,returning=”returnResult”)publicvoidafterReturn(JoinPointjoinPoint,ObjectreturnResult){
System.out.println(“afterReturn办法:{“+joinPoint.getSignature().getDeclaringTypeName()+”.”+joinPoint.getSignature().getName()+”}履行回来的结果是:{“+returnResult+”}。”);
System.out.println(“”);
}/***办法出现反常履行*/@AfterThrowing(value=”pointCut()”,throwing=”ex”)publicvoidafterThrowing(JoinPointjoinPoint,Exceptionex){
System.out.println(“afterThrowing办法:{“+joinPoint.getSignature().getDeclaringTypeName()+”.”+joinPoint.getSignature().getName()+”}发作反常:”+ex);
}
}
五、第五步:测试
importorg.junit.Test;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importcom.lqy.spring.aop.Calculator;importcom.lqy.spring.config.AopConfig;publicclassTestAop{privateAnnotationConfigApplicationContextac=newAnnotationConfigApplicationContext(AopConfig.class);
@TestpublicvoidtestDiv(){
Calculatorcal=ac.getBean(Calculator.class);//Calculator类需要在spring容器才干运用aop//System.out.println(cal.div(3,0));System.out.println(cal.add(3,2));
System.out.println(cal.divInteger(3,0));
}
}
测试结果
===============================================================before办法:{com.lqy.spring.aop.Calculator.add}开端履行:
办法参数是:{[3.0,2.0]}
加法运算
after办法:{com.lqy.spring.aop.Calculator.add}履行完毕。
afterReturn办法:{com.lqy.spring.aop.Calculator.add}履行回来的结果是:{5.0}。5.0
===============================================================before办法:{com.lqy.spring.aop.Calculator.divInteger}开端履行:
办法参数是:{[3,0]}
除法运算
after办法:{com.lqy.spring.aop.Calculator.divInteger}履行完毕。
afterThrowing办法:{com.lqy.spring.aop.Calculator.divInteger}发作反常:java.lang.ArithmeticException:/byzero

未经允许不得转载:IT技术网站 » spring aop运用,spring aop注解,Spring切面编程
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载