Spring-AOP自动创建代理之BeanNameAutoProxyCreator实例

时间:2022-01-26 01:29:19

实例

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

Spring-AOP自动创建代理之BeanNameAutoProxyCreator实例

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

如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>
<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">
    
    <!-- 配置切面:静态方法匹配切面 -->
    
    <!-- Waiter目标类 -->
    <bean id="waiterTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter"/>
    <!-- Seller目标类 -->
    <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,配置公共的信息 -->
    <bean id="parent" abstract="true"
        class="org.springframework.aop.framework.ProxyFactoryBean"
        p:interceptorNames="greetAdvicesor"
        p:proxyTargetClass="true"/>
    <!-- waiter代理 -->
    <bean id="waiter" parent="parent" p:target-ref="waiterTarget"/>
    <!-- seller代理 -->
    <bean id="seller" parent="parent" p:target-ref="sellerTarget"/>
    
</beans>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<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名称自动创建代理 -->
    
    <!-- 目标Bean -->
    <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"/>
    
    <!-- 代理      p:beanNames="waiter,seller" -->
    <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也会被自动代理器创建代理,为了保险起见,可以使用
<property name="beanNames" value="waiter,seller">的方式限定范围。

一般不会为FactoryBean的Bean创建代理,如果刚好有这样一个需求,这需要在beanNames中指定添加 的Bean 名 称 , 如 ‘ <property name="beanNames"value" 的Bean名称,如`<property name="beanNames" value=" 的Bean名称,如‘<propertyname="beanNames"value="waiter">`

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

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

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

测试类如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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的 全部方法都被织入了增强。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://artisan.blog.csdn.net/article/details/77466466