日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Spring AOP系列之三:環(huán)繞代理

系統(tǒng) 2673 0
通過實現(xiàn) org.aopalliance.intercept.MethodInterceptor 接口來實現(xiàn)環(huán)繞通知:

    
public class CarAroundProxy implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("Skoda 4S shop");
		Object result = invocation.proceed();
		System.out.println("Give 200 maintenance coupon via around proxy");
		return result;
	}
}

  


Spring配置文件beans-around-proxy.xml:
    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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"
  default-autowire="byName">

  <bean id="car" class="com.john.spring.aop.Car">
  	<property name="name" value="Superb" />
	<property name="price" value="220000" />
  </bean>
  
  <bean id="carAroundProxy" class="com.john.spring.aop.CarAroundProxy" />
  
  <!-- 通過ProxyFactoryBean來生成實現(xiàn)指定接口,攔截指定對象方法的代理類 -->
  <bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
  	<property name="proxyInterfaces"><!-- 代理需實現(xiàn)的接口 -->
  		<value>com.john.spring.aop.Vehicle</value>
	</property>
	<property name="interceptorNames"><!-- 攔截器名稱列表 -->
		<list>
			<value>carAroundProxy</value>
		</list>
	</property>
	<property name="target"><!-- 目標(biāo)對象 -->
		<ref bean="car" />
	</property>
  </bean>
</beans>

  


測試:
    
public static void main(String[] args) {
	ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "beans-around-proxy.xml" });
	Vehicle vehicle = (Vehicle) ctx.getBean("proxyBean");
	vehicle.info();
}

  


輸出:
Skoda 4S shop
Car name: Superb, price: 220000
Give 200 maintenance coupon via around proxy

調(diào)試程序,vehicle使用JDK的代理生成:
Spring AOP系列之三:環(huán)繞代理

Spring AOP系列之三:環(huán)繞代理


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宜州市| 德清县| 乐陵市| 温泉县| 静乐县| 玛曲县| 广元市| 宁乡县| 平远县| 四会市| 博兴县| 喀喇| 丁青县| 西充县| 博爱县| 靖州| 姜堰市| 灵武市| 凉山| 泰安市| 蕉岭县| 金乡县| 阳西县| 丁青县| 高密市| 得荣县| 刚察县| 酉阳| 吉隆县| 伊吾县| 庆安县| 汤原县| 老河口市| 梁平县| 晋州市| 松潘县| 临澧县| 舒城县| 阜新市| 万州区| 万安县|