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);
}
}
}
}
}
上一篇: spring cron表达式