---
title: "BeanNameAutoProxyCreator实现AOP"
author: "willwen"
date: "2013-11-18T16:23:19Z"
tags: []
categories: ["企业架构"]
slug: "beannameautoproxycreator-aop"
draft: false
lastmod: "2013-12-23T15:46:43Z"
id: 1976533
url: "https://willwen.iteye.com/blog/1976533"
---

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

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);  
}  
}  
}  
}  
}