BeanNameAutoProxyCreator实现AOP

Spring提供可以根据beanName匹配后进行自动代理的解决方法:
示例如下:
系统日志记录功能模块:
  <bean id="autoActionLogInterceptor"
         class="*.*.*.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="beanNames">
<list>
<value>*Action</value><!--代理所有action-->
</list>
      </property>
      <property name="interceptorNames">
<list>
<value>actionAfter</value>
<value>actionThrow</value>
<value>actionBefore</value>
</list>
      </property>
    </bean>
切面配置:
<bean id="actionAfter"  class="*.*.*.aspect.advice.ActionLogAfterAdvice">
       <property name="logService">
<ref bean="logService" />
      </property>
</bean>  
<bean id="actionThrow"  class="*.*.*.aspect.advice.ActionLogThrowAdvice">  
        <property name="logService">
<ref bean="logService" />
      </property>
</bean>
<bean id="actionBefore"  class="*.*.*.aspect.advice.ActionLogBeforeAdvice"> 
         <property name="logService">
<ref bean="logService" />
      </property>
               <property name="tellerAuthorizationUtil">
<ref bean="tellerAuthorizationUtil" />
      </property>
</bean>

public class ActionLogThrowAdvice implements ThrowsAdvice {
Logger logger = Logger.getLogger(this.getClass());
LogService logService;
public LogService getLogService() {
return logService;
}
public void setLogService(LogService logService) {
this.logService = logService;
}
public void afterThrowing(Method m,Object[] args,Object target,Exception ex){
// String className=target.getClass().getName();
// String methodName=m.getName();
ServiceRequest request=(ServiceRequest)args[0];
Object tellerObj =  request.getSessionDTO();
if(tellerObj!=null){
if(tellerObj instanceof TellerOpLogDTO){
TellerOpLogDTO tellerOpLog=(TellerOpLogDTO)tellerObj;
tellerOpLog.setRequestResult("failure");
tellerOpLog.setErrorDescription(ex.getMessage());
try {
logService.recordTellerOpLog(tellerOpLog);
} catch (Exception e) {
this.logger.error("ActionLogThrowAdvice.afterThrowing写日志时发生错误",e);
}
}
}
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值