`

使用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
分享到:
评论
2 楼 wrq_mimi 2014-12-12  
1 楼 Luob. 2014-07-06  
不行啊 ,我这里报错了
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'u_chinese' must be of type [cn.sh.springmvc.model.Chinese], but was actually of type [com.sun.proxy.$Proxy10]

相关推荐

    Spring实现自动代理Demo

    Spring实现自动代理Demo,BeanNameAutoProxyCreator的运用,学习参考原理

    spring in action英文版

     1.1 为什么使用Spring  1.1.1 J2EE开发者的一天  1.1.2 Spring的承诺  1.2 Spring是什么  1.3 开始Spring之旅  1.4 理解反向控制  1.4.1 依赖注入  1.4.2 IoC应用  1.4.3 企业级应用中的...

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

    12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的事务划分 12.2.8. 事务管理策略 12.2.9. 容器资源 vs 本地资源 12.2.10. 在应用服务器...

    开源框架 Spring Gossip

    &lt;br&gt;AOP 入门 AOP 的观念与术语都不是很直觉,可以先从代理机制(Spring 实现 AOP 的一种方式)来看看实际的例子,从而了解 AOP 的观念与各种术语。 从代理机制初探 AOP 动态代理 &lt;br&gt;AOP 观念与...

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

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

    AOP usage -- BeanNameAutoProxyCreator usage

    NULL 博文链接:https://tomboxfan.iteye.com/blog/350398

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

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

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

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

    SPRING API 2.0.CHM

    BeanNameAutoProxyCreator BeanNameAware BeanNameUrlHandlerMapping BeanNameViewResolver BeanNotOfRequiredTypeException BeanPostProcessor BeanPropertyBindingResult BeanPropertySqlParameterSource ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    struts spring hibernate完整环境配置

    本程序是struts spring hibernate的完整环境配置, 包括: struts1.3 spring 2.0 hibernate 3.1 的所有完整的包。 BeanNameAutoProxyCreator事务处理 中文乱码解决 MD5程序加密 AJAX验证码等 ...

    struts spring hibernate完整环境配置4/4

    本程序是struts spring hibernate的完整环境配置, 包括: struts1.3 spring 2.0 hibernate 3.1 的所有完整的包。 BeanNameAutoProxyCreator事务处理 中文乱码解决 MD5程序加密 AJAX验证码等 ...

    struts spring hibernate完整环境配置3/4

    本程序是struts spring hibernate的完整环境配置, 包括: struts1.3 spring 2.0 hibernate 3.1 的所有完整的包。 BeanNameAutoProxyCreator事务处理 中文乱码解决 MD5程序加密 AJAX验证码等 ...

    spring学习笔记

    Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    struts2驱动包

    nested exception is java.lang.AbstractMethodError: org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator.postProcessAfterInstantiation(Ljava/lang/Object;Ljava/lang/String;)Z at org....

Global site tag (gtag.js) - Google Analytics