spring框架AOP配置
阿新 • • 發佈:2018-12-18
第一步:匯入包 aopalliance-1.0.jar aspectjweaver-1.8.13.jar spring-aop-3.2.13.RELEASE.jar
spring開頭的包自帶的有 aopalliance,aspectjweaver去https://repo.spring.io/下載 還加上ioc的5個包:
commons-logging-1.2.jar spring-beans-3.2.13.RELEASE.jar spring-context-3.2.13.RELEASE.jar spring-core-3.2.13.RELEASE.jar spring-expression-3.2.13.RELEASE.jar
第二步:配置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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="usLogger" class="com.big3.aop.UserServiceLogger"/> <!-- 日誌增強注入 --> <aop:config> <!--定義切入點--> <aop:pointcut id="pointcut" expression="execution(public void eat(com.big3.pojo.User))"/> <!--織入增強處理,ref:增強處理物件,注入的日誌類--> <aop:aspect ref="usLogger"> <!--前置增強處理,method:被織入的方法,pointcut-ref:切入點--> <aop:before method="before" pointcut-ref="pointcut"/> <!--後置增強處理,method:被織入的方法,pointcut-ref:切入點,returning:指定返回值的屬性名為result--> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> </aop:aspect> </aop:config> <bean id="errorLogger" class="com.big3.aop.ErrorLogger"/> <!-- 異常增強注入 --> <aop:config> <!-- 定義切入點 --> <aop:pointcut id="point" expression="execution(public void save(com.big3.pojo.User))"/> <!-- 織入增強處理,ref:增強處理物件,注入的異常類 --> <aop:aspect ref="errorLogger"> <!--after-throwing:異常增強, method:被織入的方法,pointcut-ref:切入點,throwing:為e引數注入異常例項 --> <aop:after-throwing method="afterThrowing" pointcut-ref="point" throwing="e"/> </aop:aspect> </aop:config> <bean id="afterLogger" class="com.big3.aop.AfterLogger" /> <!-- 最終增強注入 --> <aop:config> <!-- 定義切入點 --> <aop:pointcut id="points" expression="execution(public void eat(int ,String))"/> <!-- 織入增強處理,ref:增強處理物件,注入的異常類--> <aop:aspect ref="afterLogger"> <!--after:最終增強, method:被織入的方法,pointcut-ref:切入點 ;--> <aop:after method="after" pointcut-ref="points"/> </aop:aspect> </aop:config> <bean id="aroundLogger" class="com.big3.aop.AroundLogger"/> <!-- 環繞增強 --> <aop:config> <!-- 定義切入點 --> <aop:pointcut id="pointse" expression="execution(public int saves(com.big3.pojo.User))"/> <aop:aspect ref="aroundLogger"> <aop:around method="aroundLogger" pointcut-ref="pointse"/> </aop:aspect> </aop:config> </beans>
如果用註解配置檔案如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--掃描包中註解標註的類--> <context:component-scan base-package="com.big3.dao,com.big3.pojo,com.big3.aop"/> <!--註冊Aspectj代理物件--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
第三步:JAVA應用,如果不用註解,就不用註解標示
日誌增強
@Aspect
@Component("usLogger")
public class UserServiceLogger {
private static Logger log=Logger.getLogger(UserServiceLogger.class);
//定義切入點方法
@Pointcut("execution(public void eat(com.big3.pojo.User))")
public void pointcut(){}
//前置增強
@Before("pointcut()")
public void before(JoinPoint jp){
log.info("呼叫"+jp.getTarget()+"的"+jp.getSignature()+"方法,方法引數:\"+Arrays.toString(jp.getArgs()));\n" +
"\t}"+Arrays.toString(jp.getArgs()));
}
//後置增強
@AfterReturning(pointcut="pointcut()",returning="result")
public void afterReturning(JoinPoint jp,Object result){
log.info("呼叫"+jp.getTarget()+"的"+jp.getSignature()+"方法,方法返回值:"+result);
}
異常增強
@Aspect //增強處理
@Component("errorLogger")
public class ErrorLogger {
private static Logger log=Logger.getLogger(UserServiceLogger.class);
//定義切入點方法
@Pointcut("execution(public void save(com.big3.pojo.User))")
public void pointcut(){}
//定義異常增強方法
@AfterThrowing(pointcut="pointcut()",throwing="e")
public void afterThrowing(JoinPoint jp,RuntimeException e){
log.error(jp.getSignature().getName()+"方法發生異常,"+e);
}
}
最終增強
//最終增強,不管是否發生異常都執行,用於釋放資源
@Aspect //增強處理
@Component("afterLogger")
public class AfterLogger {
private static Logger log=Logger.getLogger(UserServiceLogger.class);
//定義切入點方法
@Pointcut("execution(public void eat(int ,String))")
public void pointcut(){}
//定義最終增強方法
@After("pointcut()")
public void after(JoinPoint jp){
log.info(jp.getSignature().getName()+"方法結束了");
}
}
環繞增
//環繞增強
@Aspect //增強處理
@Component("aroundLogger")
public class AroundLogger {
private static final Logger log=Logger.getLogger(AroundLogger.class);
//定義環繞增強方法
@Around("execution(public int saves(com.big3.pojo.User))")
public Object aroundLogger(ProceedingJoinPoint jp)throws Throwable{
User user=(User)jp.getArgs()[0];
log.info("呼叫"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法入參"+user.getName());
try {
int result=(int)jp.proceed();//執行目標方法並獲得返回值
log.info("呼叫"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法返回值"+(result+200));
return result;
} catch (Throwable throwable) {
log.error(jp.getSignature().getName()+"方法發生異常"+throwable);
throw throwable;
}finally {
log.info(jp.getSignature().getName()+"方法結束執行.");
}
}
}