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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 司徒正美
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
Help Net Security
Help Net Security
N
News and Events Feed by Topic
O
OpenAI News
L
LangChain Blog
F
Full Disclosure
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
Cloudbric
Cloudbric
W
WeLiveSecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
Attack and Defense Labs
Attack and Defense Labs
PCI Perspectives
PCI Perspectives
TaoSecurity Blog
TaoSecurity Blog
AI
AI
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
T
Threatpost
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Last Week in AI
Last Week in AI
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
P
Proofpoint News Feed
Latest news
Latest news
S
SegmentFault 最新的问题
J
Java Code Geeks
T
Threat Research - Cisco Blogs
H
Help Net Security
P
Privacy International News Feed

博客园 - 走到天亮

设计模式之“适配器模式” 设计模式之“门面模式” 设计模式之“抽象工厂模式” 设计模式之“单例模式” 设计模式之“代理模式” 设计模式之“策略模式” 《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");
    }