`

使用BeanNameAutoProxyCreator实现spring的自动代理

阅读更多

提到代理,我们可以使用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

http://esffor.iteye.com/blog/96128

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Spring实现自动代理Demo

    一、Spring自动代理简介 自动代理是Spring AOP的核心功能之一,它创建了一个代理对象来包装原始的bean。当调用代理对象的方法时,Spring会先执行一些预定义的行为(如切面),然后转发调用到实际的目标对象。这使得...

    AOP usage -- BeanNameAutoProxyCreator usage

    `BeanNameAutoProxyCreator`是Spring AOP实现中的一种代理创建器,它根据bean的名称来决定是否对bean进行代理处理。在本篇文章中,我们将深入探讨`BeanNameAutoProxyCreator`的使用方法及其背后的原理。 首先,`...

    spring 事务代理配置

    2. **使用BeanNameAutoProxyCreator**:根据Bean名称自动创建事务代理,这需要对Spring AOP有更深入的理解。 3. **使用DefaultAdvisorAutoProxyCreator**:与`BeanNameAutoProxyCreator`类似,但其配置的可读性可能...

    spring代理详解.txt

    `DefaultAdvisorAutoProxyCreator`是Spring中最常用的自动代理创建者,它会自动将所有符合条件的advisor应用到所有的bean上。与`BeanNameAutoProxyCreator`不同,它不依赖于bean名称,而是依赖于advisor的pointcut...

    Spring-Reference_zh_CN(Spring中文参考手册)

    7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.1.1. BeanNameAutoProxyCreator 7.9.1.2. DefaultAdvisorAutoProxyCreator 7.9.1.3. AbstractAdvisorAutoProxyCreator 7.9.2. 使用元数据驱动...

    spring 事务(6中配置完全降解)

    `DefaultAdvisorAutoProxyCreator`是Spring AOP中用于自动创建代理的组件,它会寻找所有`Advisor`(包括事务增强`Advisor`),并将它们应用到相关的bean上。这样,我们可以通过定义`Pointcut`来决定哪些方法需要...

    开源框架 Spring Gossip

    IntroductionInterceptor DelegatingIntroductionInterceptor Autoproxing 自动代理可以让您不用为每一个要被 Advised 的 Target 手动定义代理物件,透过 Bean 名称或是 Pointcut 的比对,自动为...

    Spring.3.x企业应用开发实战(完整版).part2

    经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用Spring的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...

    Spring3.x企业应用开发实战(完整版) part1

    经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用Spring的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...

    Springboot源码 AbstractAdvisorAutoProxyCreator解析

    1. 基于Bean配置名规则的自动代理生成器:BeanNameAutoProxyCreator是这种类型的实现类,它允许为一组特定配置名的Bean自动创建代理实例。 2. 基于Advisor匹配机制的自动代理创建器:...

    Spring声明式事务和@Aspect的拦截顺序问题的解决

    拦截顺序问题的原因是由于 Spring 框架中有多种自动代理方式的存在。Spring 框架中有三种自动代理方式: 1. 通过 Bean 的名称自动创建代理,实现类 BeanNameAutoProxyCreator 2. 根据 Bean 中的 AspectJ 注解自动...

    spring1.2申明式事务.txt

    通过配置事务管理器、事务拦截器以及自动代理创建器,开发者可以轻松地为应用程序添加事务支持,而无需编写复杂的事务代码。这种方式不仅提高了代码的可读性和可维护性,还降低了事务管理的复杂度,使得开发者可以...

    Spring在Transaction事务传播行为种类

    了解并正确使用这些事务传播行为,可以帮助我们更好地设计和实现复杂的应用程序,确保数据的一致性和事务的完整性。在实际开发过程中,应根据具体的业务需求来选择合适的事务传播行为类型,以达到最佳的效果。

    spring拦截器的简单例子.docx

    最后,`autoproxy` Bean 是一个自动代理创建器,它会为 `purviewimpl` 这个 Bean 创建一个代理,这个代理会在调用 `logincheck()` 方法时,先执行 `BeforeMethod` 的 `before()` 方法。 总结来说,这个例子展示了...

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    --定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理--&gt; class="com.service.impl.UserManagerImpl"&gt; singleton="false"&gt; &lt;/beans&gt;

    事物管理配置文件.txt

    **四、自动代理的配置** 为了使事务拦截器能够应用于业务层的方法调用,我们需要使用`BeanNameAutoProxyCreator`来创建AOP代理: ```xml class="org.springframework.aop.framework.autoproxy....

Global site tag (gtag.js) - Google Analytics