---
title: "使用BeanNameAutoProxyCreator实现spring的自动代理"
author: "zlk"
date: "2010-06-19T11:13:54Z"
tags: ["AOP", "Bean", "Spring", "XML", "工作"]
categories: ["企业架构"]
slug: "beannameautoproxycreator-spring"
draft: false
lastmod: "2010-06-19T11:13:54Z"
id: 694423
url: "https://zlk.iteye.com/blog/694423"
---

提到代理，我们可以使用ProxyBeanFactory，并配置proxyInterfaces,target和interceptorNames实现，但如果需要代理的bean很多，无疑会对spring配置文件的编写带来繁重的工作

Spring为我们提供了，根据beanName匹配后进行自动代理的解决方法

业务接口

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package AutoProxyOne;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public interface Shopping ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  public String buySomething(String type);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  public String buyAnything(String type);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  public String sellSomething(String type);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  public String sellAnything(String type);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 业务实现类A，作为配置文件中的buyBean：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package AutoProxyOne;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ShoppingImplA implements Shopping ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Customer customer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public Customer getCustomer() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return customer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public void setCustomer(Customer customer) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.customer = customer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String buySomething(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println(this.getCustomer().getName()+" bye "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String buyAnything(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       System.out.println(this.getCustomer().getName()+" bye "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String sellAnything(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println(this.getCustomer().getName()+" sell "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String sellSomething(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println(this.getCustomer().getName()+" sell "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

 业务实现类B，作为配置文件中的sellBean：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package AutoProxyOne;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ShoppingImplB implements Shopping ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private Customer customer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public Customer getCustomer() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return customer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public void setCustomer(Customer customer) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.customer = customer;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String buySomething(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println(this.getCustomer().getName()+" bye "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String buyAnything(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       System.out.println(this.getCustomer().getName()+" bye "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String sellAnything(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println(this.getCustomer().getName()+" sell "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String sellSomething(String type) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println(this.getCustomer().getName()+" sell "+type+" success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           return null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

切面通知：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package AutoProxyOne;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.lang.reflect.Method;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.aop.MethodBeforeAdvice;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//前置通知  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class WelcomeAdvice implements MethodBeforeAdvice ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public void before(Method method, Object\[] args, Object obj)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            throws Throwable ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("Hello welcome to bye ");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

配置文件：

其中beanNames为buy\*,意味着所有以buy开头的bean,都被spring容易自动代理，执行相应的切面通知

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" &gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;beans&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;bean id="WelcomeAdvice" class="AutoProxyOne.WelcomeAdvice"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;property name="beanNames"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;list&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       &lt;value&gt;buy\*&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;/list&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;property name="interceptorNames"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;list&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        &lt;value&gt;WelcomeAdvice&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;/list&gt;   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;bean id="buyBean" class="AutoProxyOne.ShoppingImplA"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="customer"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;ref bean="customer"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;bean id="sellBean" class="AutoProxyOne.ShoppingImplB"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;property name="customer"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;ref bean="customer"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;/property&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;bean id="customer" class="AutoProxyOne.Customer"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;constructor-arg index="0"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;gaoxiang&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/constructor-arg&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;constructor-arg index="1"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;26&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/constructor-arg&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/bean&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/beans&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

测试代码：

在测试代码中，我们的buyBean打印两条买的信息，sellBean打印两条卖的信息，可以看到buyBean执行的方法已经进行了切面处理

需要注意的是，如果使用自动代码，则获得Spring Bean工厂要用

ApplicationContext ctx=new FileSystemXmlApplicationContext(filePath);

而不能用

BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));

原因我想是因为BeanFactory在初始化时并不实例化单例的Bean,而ApplicationContext则在初始化时候全部实例化了Bean,自动代理需要在初始化时候定义好代理关系

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package AutoProxyOne;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.beans.factory.BeanFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.beans.factory.xml.XmlBeanFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.context.ApplicationContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.context.support.FileSystemXmlApplicationContext;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.core.io.FileSystemResource;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.springframework.aop.support.RegexpMethodPointcutAdvisor;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class TestAdvisor ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void main(String\[] args) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String filePath=System.getProperty("user.dir")+File.separator+"AutoProxyOne"+File.separator+"hello.xml";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ApplicationContext ctx=new FileSystemXmlA