惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
U
Unit 42
W
WeLiveSecurity
博客园 - Franky
Recent Announcements
Recent Announcements
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
月光博客
月光博客
The Cloudflare Blog
Spread Privacy
Spread Privacy
腾讯CDC
P
Privacy International News Feed
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
T
Troy Hunt's Blog
小众软件
小众软件
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Microsoft Security Blog
Microsoft Security Blog
L
Lohrmann on Cybersecurity
Webroot Blog
Webroot Blog
Y
Y Combinator Blog
量子位
P
Palo Alto Networks Blog
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
IT之家
IT之家
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
G
Google Developers Blog
S
Secure Thoughts

博客园 - 走到天亮

设计模式之“适配器模式” 设计模式之“门面模式” 设计模式之“抽象工厂模式” 设计模式之“单例模式” 设计模式之“代理模式” 设计模式之“策略模式” 《C# to IL》第三章 选择和循环 《C# to IL》第二章 IL基础 《C# to IL》第一章 IL入门 淘宝下单高并发解决方案(转载) java linux 配置环境 Spring Aop之(二)--Aop 切面声明和通知 Spring aop Spring RegexpMethodPointcutAdvisor和NameMatchMethodPointcutAdvisor CentOS的IP配置专题 Spring Bean属性绑定Bean返回值 【阿里的感悟】质量该如何做? .(转载) Ubuntu开机自动启动script(2) Ubuntu开机自动启动Script
Spring BeanNameAutoProxyCreator 与 ProxyFactoryBean
走到天亮 · 2012-08-14 · via 博客园 - 走到天亮

一般我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的编写带来繁重的工作,这时就该BeanNameAutoProxyCreator出场了。

(一)ProxyFactoryBean属性介绍 

target:代理的目标类
proxyInterfaces:代理类应该实现的接口列表
interceptorNames:需要应用到目标对象上的通知Bean的名字.可以是拦截器,advisor和其他通知类型的名字。这个属性必须按照在BeanFactory中的顺序设置
singleton:单例
aopProxyFactory:使用的ProxyFactoryBean实现。Spring带有两种实现(JDK动态代理和CGLIB)。通常不需要使用这个属性
exposeProxy:目标对象是否需要得到当前的代理。通过调用AopContext.getCurrentProxy实现。
frozen:一旦工厂被创建,是否可以修改代理的通知。当设置为true时,在运行时就不能修改ProxyFactoryBean了。通常不需要使用这个属性。
optimize:是否对创建的代理进行优化(只适用于CGLIB)
ProxyTargetClass:是否代理目标类,而不是实现接口。只能在使用CGLIB时使用

 

现在说下ProxyBeanFactory的用法以及配置:

 ITest.class

public interface ITest {
    void tst();
    void tst(int status,String name);
}

 TestProxyFactoryBean.class

public class TestProxyFactoryBean implements ITest {

    @Override
    public void tst() {
        // TODO Auto-generated method stub
        System.out.println("执行方法.");
    }

    @Override
    public void tst(int status, String name) {
        System.out.println("tst(int status, String name)" );
        
    }

}

 LoggerAdvice.class

/**
 * 日志代理
 * 
@author Administrator
 *
 
*/
public class LoggerAdvice implements MethodBeforeAdvice, AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) throws Throwable {
         Logger logger = Logger.getLogger(target.getClass());
          if(returnValue != null){
              logger.debug("+-------Return : " + returnValue.toString());
          }
        
    }

    @Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        // TODO Auto-generated method stub
          Logger logger = Logger.getLogger(target.getClass());
          logger.debug("+Class : "+ target.getClass().getName());
          logger.debug("+-------Method : "+ method.getName());
          for(int i=0; i<args.length; i++){
           logger.debug(" +-arg"+ i + " : " + args[i].toString());
          }
        
    }

}

Xml配置:

<bean class="spring.aop.TestProxyFactoryBean" id="testproxyfactorybean"/> 

<bean class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces" value="spring.aop.ITest"></property>
    <property name="interceptorNames" >

     <list>
      <value>loggerAdvice</value>
     </list>

    </property>

    <property name="target" ref="testproxyfactorybean"></property>
    </bean>

Program.class
public static void main(String[] args){
        context.getBean("proxyBean",ITest.class).tst();
        context.getBean("proxyBean",ITest.class).tst(1,"123");
    }

执行结果:

2012-08-14 14:57:30 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'proxyBean'
2012-08-14 14:57:30 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'loggerAdvice'
2012-08-14 14:57:30 [org.springframework.aop.framework.ProxyFactoryBean]-[DEBUG] Advice has changed; recaching singleton instance
2012-08-14 14:57:30 [org.springframework.aop.framework.JdkDynamicAopProxy]-[DEBUG] Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [spring.aop.TestProxyFactoryBean@11978b]
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +Class : spring.aop.TestProxyFactoryBean
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +-------Method : tst
执行方法.
2012-08-14 14:57:30 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'proxyBean'
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +Class : spring.aop.TestProxyFactoryBean
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +-------Method : tst
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG]  +-arg0 : 1
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG]  +-arg1 : 123
tst(int status, String name)

(二)BeanNameAutoProxyCreator属性介绍

target:代理的目标类

beanNames:需要代理的bean的列表

interceptorNames:需要应用到目标对象上的通知Bean的名字.可以是拦截器,advisor和其他通知类型的名字。这个属性必须按照在BeanFactory中的顺序设置

 

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="interceptorNames">
   <list>
    <value>loggerAdvice</value>
   </list>
  </property>
  <property name="beanNames">
   <list>
    <idref local="testproxyfactorybean" />
   </list>
  </property>
 </bean>

调用代码:

public static void main(String[] args){
       
        context.getBean("testfunc",TestFunc.class).test("project", 100);
        context.getBean("testfunc",TestFunc.class).test1("project1");
    }