提到代理,我们可以使用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. 【POJ 3525】Most Distant Point from the Sea(直线平移、半平面交)

    按逆时针顺序给出n个点,求它们组成的多边形的最大内切圆半径. 二分这个半径,将所有直线向多边形中心平移r距离,如果半平面交不存在那么r大了,否则r小了. 平移直线就是对于向量ab,因为是逆时针的,向中 ...

  2. DIRECTORY_SEPARATOR:PHP 系统分隔符常量

    今天在nginx部署项目,在浏览器输入http://127.0.0.2/index.php/system/category/?action=list 老是提示error nginx配置没有问题,下了其 ...

  3. Leetcode 270. Closest Binary Search Tree Value

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  4. 【BZOJ-2055】80人环游世界 上下界费用流 (无源无汇最小费用最大流)

    2055: 80人环游世界 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 321  Solved: 201[Submit][Status][Discus ...

  5. C# winform窗体设计-数据库连接

    本篇文章内容主要是小编上课所学的总结 最近小编在学习C#中的数据库管理方面,主要学习到数据库的增删改查,查询学生平均分,最低分,最高分等操作 [本篇文章中小编主要讲解数据库的连接] 在C#中使用数据库 ...

  6. Windows、Linux下文件操作(写、删除)错误的产生原因、及解决方法

    catalog . 引言 . Linux平台上涉及的File IO操作 . Windows平台上涉及的File IO操作 0. 引言 本文试图讨论在windows.linux操作系统上基于C库进行文件 ...

  7. iOS WebView调用JS的一个小坑

    假如调用一个函数,传入的参数为String,要以这样的格式传入: let resultStr="1234" self.webView.stringByEvaluatingJavaS ...

  8. 第六次作业——利用MFC实现计算器图形界面以及简单四则运算表达式批处理

    参考资料:      1.MFC响应键盘      2.计算器实例      3.MFC文件对话框      4.MFCUpdateData()函数的使用      5.MFC教程      6.wi ...

  9. android service 本地 远程 总结

    android编写Service入门 android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service) ...

  10. SSH 学习总结-01 SSH整合环境

    一 Struts2+Spring3+Hibernate4+Maven 整合环境 1 开发工具 1)JDK下载地址:http://www.oracle.com/technetwork/java/java ...