Spring-AOP 自动创建代理之BeanNameAutoProxyCreator

  • 导读
  • 实例

导读

Spring-AOP 自动创建代理

Spring-AOP 自动创建代理之BeanNameAutoProxyCreator

Spring-AOP 自动创建代理之DefaultAdvisorAutoProxyCreator

Spring-AOP 自动创建代理之AnnotationAwareAspectJAutoProxyCreator


实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

Spring-AOP 自动创建代理之BeanNameAutoProxyCreator_第1张图片

在 Spring-AOP 静态普通方法名匹配切面 案例中,我们通过配置两个ProxyFactoryBean分别为waiter和seller的Bean创建代理对象,如下


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    

    
    <bean id="waiterTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter"/>
    
    <bean id="sellerTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Seller"/>

    
    <bean id="greetBeforeAdvice" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetBeforeAdivce"/>

    
    <bean id="greetAdvicesor" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetingAdvisor"
        p:advice-ref="greetBeforeAdvice"/> 

    
    <bean id="parent" abstract="true"  
        class="org.springframework.aop.framework.ProxyFactoryBean"
        p:interceptorNames="greetAdvicesor"
        p:proxyTargetClass="true"/>

    
    <bean id="waiter" parent="parent" p:target-ref="waiterTarget"/>
    
    <bean id="seller" parent="parent" p:target-ref="sellerTarget"/>

beans>

下面我们通过BeanNameAtuoProxyCreator以更优雅更快捷的方式完成相同的功能。


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    

    
    <bean id="waiter" class="com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Waiter"/>
    <bean id="seller" class="com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Seller"/>

    
    <bean id="greetingBeforeAdvice" class="com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.GreetingBeforeAdvice"/>

    
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
        p:beanNames="*er"
        p:interceptorNames="greetingBeforeAdvice"
        p:optimize="true"/>
beans>

BeanNameAutoProxyCreator有一个beanNames属性,它允许用户指定一组需要自动代理的Bean名称,Bean名称可以使用*通配符。

假设Spring容器中有waiter和seller外还有其他的bean, 就可以通过beanNames属性设定为“*er” 使wiater和seller这两个bean被自动代理。 当然,如果还有其他以er结尾的bean也会被自动代理器创建代理,为了保险起见,可以使用
的方式限定范围。

一般不会为FactoryBean的Bean创建代理,如果刚好有这样一个需求,这需要在beanNames中指定添加$的Bean名称,如

BeanNameAutoProxyCreator的interceptorNames属性指定一个或者多个Bean的名称。

此外还有一个常用的optimize属性,如果将此属性设置为true,则将强制使用CGLib动态代理技术。

通过这样的配置后,容器在创建waiter和seller Bean的实例是,就会自动为他们创建代理对象,而这一操作对使用者来讲完全是透明的。

测试类如下:

package com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanNameAutoProxyCreatorTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/aop/spring/advisor/autoCreateProxy/BeanNameAutoProxyCreator/conf-beanNameAutoProxy.xml");

        Waiter waiter = ctx.getBean("waiter", Waiter.class);
        waiter.greetTo("XiaoGongJiang");
        waiter.serverTo("XiaoGongJiang");

        System.out.println("\n");

        Seller seller = ctx.getBean("seller", Seller.class);
        seller.greetTo("XiaoGongJiang");
        seller.serverTo("XiaoGongJiang");
    }
}

运行结果如下:

2017-08-21 16:12:48,086  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5f0101fb: startup date [Mon Aug 21 16:12:48 BOT 2017]; root of context hierarchy
2017-08-21 16:12:48,204  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/autoCreateProxy/BeanNameAutoProxyCreator/conf-beanNameAutoProxy.xml]
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Waiter.greetTo
How are you XiaoGongJiang ?
Waiter Greet To XiaoGongJiang
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Waiter.serverTo
How are you XiaoGongJiang ?
Waiter Server To XiaoGongJiang


Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Seller.greetTo
How are you XiaoGongJiang ?
Seller Greet To XiaoGongJiang
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Seller.serverTo
How are you XiaoGongJiang ?
Seller Server To XiaoGongJiang

通过输出信息可以得知,从容器返回的Bean的 全部方法都被织入了增强。

你可能感兴趣的:(【Spring-AOP】,Spring-AOP基础手札)