溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring-AOP怎么自動創(chuàng)建代理

發(fā)布時間:2021-07-20 04:21:27 來源:億速云 閱讀:145 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Spring-AOP怎么自動創(chuàng)建代理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Spring-AOP怎么自動創(chuàng)建代理”吧!

實例

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

Spring-AOP怎么自動創(chuàng)建代理

在 Spring-AOP 靜態(tài)普通方法名匹配切面 案例中,我們通過配置兩個ProxyFactoryBean分別為waiter和seller的Bean創(chuàng)建代理對象,

如下

<?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">
	
	<!-- 配置切面:靜態(tài)方法匹配切面 -->
	
	<!-- Waiter目標(biāo)類 -->
	<bean id="waiterTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter"/>
	<!-- Seller目標(biāo)類 -->
	<bean id="sellerTarget" class="com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Seller"/>
	
	<!-- 前置增強(qiáng) -->
	<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"/> <!-- 向切面注入一個前置增強(qiáng) -->
		
	<!-- 通過父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以更優(yōu)雅更快捷的方式完成相同的功能

<?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名稱自動創(chuàng)建代理 -->
	
	<!-- 目標(biāo)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"/>
	
	<!-- 增強(qiáng) -->
	<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名稱可以使用*通配符。

假設(shè)Spring容器中有waiter和seller外還有其他的bean, 就可以通過beanNames屬性設(shè)定為“*er” 使wiater和seller這兩個bean被自動代理。 當(dāng)然,如果還有其他以er結(jié)尾的bean也會被自動代理器創(chuàng)建代理,為了保險起見,可以使用
<property name="beanNames" value="waiter,seller">的方式限定范圍。

一般不會為FactoryBean的Bean創(chuàng)建代理,如果剛好有這樣一個需求,這需要在beanNames中指定添加 的Bean 名 稱 , 如 ‘ <property name="beanNames"value" 的Bean名稱,如`<property name="beanNames" value=" 的Bean名稱,如‘<propertyname="beanNames"value="waiter">`

BeanNameAutoProxyCreator的interceptorNames屬性指定一個或者多個Bean的名稱。

此外還有一個常用的optimize屬性,如果將此屬性設(shè)置為true,則將強(qiáng)制使用CGLib動態(tài)代理技術(shù)。

通過這樣的配置后,容器在創(chuàng)建waiter和seller Bean的實例是,就會自動為他們創(chuàng)建代理對象,而這一操作對使用者來講完全是透明的。

測試類如下:

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");
	}
}

運(yùn)行結(jié)果如下:

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的 全部方法都被織入了增強(qiáng)。

到此,相信大家對“Spring-AOP怎么自動創(chuàng)建代理”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI