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

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

业务接口

package AutoProxyOne;

public interface Shopping {
  public String buySomething(String type);
  public String buyAnything(String type);
  public String sellSomething(String type);
  public String sellAnything(String type);


}

业务实现类A,作为配置文件中的buyBean:

package AutoProxyOne;

public class ShoppingImplA implements Shopping {
    private Customer customer;
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public String buySomething(String type) {
        System.out.println(this.getCustomer().getName()+" bye "+type+" success");
        return null;
    }
    
    public String buyAnything(String type) {
       System.out.println(this.getCustomer().getName()+" bye "+type+" success");
       return null;

     }
    public String sellAnything(String type) {
        System.out.println(this.getCustomer().getName()+" sell "+type+" success");
        return null;
    }
    public String sellSomething(String type) {
         System.out.println(this.getCustomer().getName()+" sell "+type+" success");
           return null;
    }

}

业务实现类B,作为配置文件中的sellBean:

package AutoProxyOne;

public class ShoppingImplB implements Shopping {
    private Customer customer;
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public String buySomething(String type) {
        System.out.println(this.getCustomer().getName()+" bye "+type+" success");
        return null;
    }
    
    public String buyAnything(String type) {
       System.out.println(this.getCustomer().getName()+" bye "+type+" success");
       return null;

     }
    public String sellAnything(String type) {
        System.out.println(this.getCustomer().getName()+" sell "+type+" success");
        return null;
    }
    public String sellSomething(String type) {
         System.out.println(this.getCustomer().getName()+" sell "+type+" success");
           return null;
    }

}

切面通知:

package AutoProxyOne;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
//前置通知
public class WelcomeAdvice implements MethodBeforeAdvice {

    public void before(Method method, Object[] args, Object obj)
            throws Throwable {
        
        System.out.println("Hello welcome to bye ");

    }

}

配置文件:

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
 <bean id="WelcomeAdvice" class="AutoProxyOne.WelcomeAdvice">
 </bean>
 
 <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="beanNames">
     <list>
       <value>buy*</value>
     </list>
   </property>
   <property name="interceptorNames">
     <list>
        <value>WelcomeAdvice</value>
     </list> 
   </property>

 </bean>
   
  <bean id="buyBean" class="AutoProxyOne.ShoppingImplA">
    <property name="customer">
      <ref bean="customer"/>
    </property>
   </bean>
 <bean id="sellBean" class="AutoProxyOne.ShoppingImplB">
    <property name="customer">
      <ref bean="customer"/>
    </property>
   </bean>


<bean id="customer" class="AutoProxyOne.Customer">
   <constructor-arg index="0">
     <value>gaoxiang</value>
   </constructor-arg>
    <constructor-arg index="1">
     <value>26</value>
   </constructor-arg>
 </bean>


</beans>

测试代码:

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

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

ApplicationContext ctx=new FileSystemXmlApplicationContext(filePath);

而不能用

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

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

package AutoProxyOne;

import java.io.File;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;


import org.springframework.aop.support.RegexpMethodPointcutAdvisor;
public class TestAdvisor {

    public static void main(String[] args) {

        String filePath=System.getProperty("user.dir")+File.separator+"AutoProxyOne"+File.separator+"hello.xml";
        
        BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));
        ApplicationContext ctx=new FileSystemXmlA

使用BeanNameAutoProxyCreator实现spring的自动代理的更多相关文章

  1. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  2. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  3. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  4. 使用注解配置Spring框架自动代理通知

    话不多说上代码 项目架构图及Lib包如下: 第二步创建业务类接口 package cn.happy.day01.entity; /** * 1.业务接口 * @author Happy * */ pu ...

  5. spring aop自动代理xml配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. spring aop自动代理注解配置之二

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. spring aop自动代理注解配置之一

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 名称自动代理生成器:BeanNameAutoProxyCreator 为了更好的测试,我放了俩个接口,俩个实现 ...

  9. Spring AOP使用整理:自动代理以及AOP命令空间

    三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...

随机推荐

  1. css-IE中的border-radius和box-shadow

    1.今天遇到一个问题,在给一个图片设置border-radius时,firefox和chrome表现正常,但是在IE中就无效,在IE中必须给图片设置一个边框,比如设置一个透明的边框就可以正常显示. 2 ...

  2. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  3. UOJ265 【NOIP2016】愤怒的小鸟

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  4. TortoiseGit与GitHub项目关联设置

    一.常规克隆GitHub上的项目: 1.在本地硬盘上放置项目的地方上[右键]->[Git 克隆]->在[url地址]上输入https的GitHub的链接,然后就是等待完成,之后即可完成拉取 ...

  5. win7下firefox浏览器不能使用

    win7下firefox浏览器不能使用,只有360浏览器才能使用. 使用360安全卫士,到更多工具里面选择"LSP修复",就可以了. 原来是因为安装了土豆加速,然后卸载导致的. 看 ...

  6. CentOS 7.0 安装配置LAMP服务器方法(Apache+PHP+MariaDB)

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: #停止firewall服务 sys ...

  7. Untiy3D - 窗口界面1

    记录Untiy3D学习中的英语单词 一.Project窗口下的英语单词 First Day Folder : 文件夹 C# Script : C#脚本 JavaScript:JS脚本 Editor T ...

  8. OC之NSString、NSMutableString学习笔记 常用方法

    NSString篇: 1.字符串连接 NSString *beijing = @"北京"; NSString *welcome = [beijing stringByAppendi ...

  9. glade2支持C++代码的输出(3)

    今天完成了glade-2生成configure.ac/Makefile.am等调整 代码为:cpp_out_4.patch.zip BaseObject类也做了一些小的调整:BaseObject.00 ...

  10. mongdb查询与排序

    db.QResult.find({'CreateDate':{'$gte' : ISODate('2016-07-01'), '$lte' : ISODate('2016-08-01')}}).sor ...