1. 程式人生 > >spring 學習筆記 引入通知

spring 學習筆記 引入通知

建立引入

引入是為需要方法的類新增屬性和方法。可以用一個已存在的類讓他實現另外的介面,維持另外的狀態(這也叫混合)。引入能夠動態的建立複合物件,提供了多型繼承的好處。

class AuditableIntroductionInterceptor extends DelegatingIntroductionInceptor{

     private Date lastModifiedDate ;
     public Date getLastModifiedDate(){}
     public void setLastModifiedDate(Date date){}
}
如果你的混合體要改變任何目標物件方法的行為的話,你就需要實現invoke()方法。
public Object invoke(MethodInvcation mi) throws Throwable{
     String name = mi.getMethod().getName();
     if(name.indexof("set") == 0){
            throw new IllegalModificationException();
     }
     return super.invoke(mi);
}
我們複寫了invoke方法,所以他攔截所有的方法的呼叫。注意我們呼叫的super.invoke()而
不是mi.proceed().這樣做是因為父類DelegatingIntroductionInterceptorkeyijiang可以
決定哪個類負責處理這個方法呼叫。這一點需要注意,當需要複寫invoke方法時,你需要呼叫
super.invoke方法來確保呼叫正確的方法。

public class MyDDI extends DelegatingIntroductionInterceptor implements
		ModifyData {
	private static final long serialVersionUID = 8244858324942778490L;
	private String data;
	public void setData(String data) {
		this.data = data;
	}

	public String getData() {
		return this.data;
	}
}
public interface ModifyData {
	public void setData(String data);
	public String getData();
}
<bean id="myDII" class="study.spring.aop.advice.MyDDI"></bean>
	<!-- 預設的引入切入點通知(代理引入攔截器,對引入通知進行包裝) -->
	<bean id="delegatingIntroductionInterceptor" class="org.springframework.aop.support.DelegatingIntroductionInterceptor">
		<constructor-arg ref="myDII"></constructor-arg>
	</bean>
	<!-- 目標物件 -->
	<bean id="welcomeServiceTarget" class="study.spring.aop.WelcomeServiceImpl"></bean>
	<!-- 物件代理 -->
	<bean id="welcomeService" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!-- 代理介面集 -->
		<property name="proxyInterfaces">
			<list>
				<value>study.spring.aop.ModifyData</value>
			</list>		
		</property>
		<!-- 攔截器名集 -->
		<property name="interceptorNames">
			<list>
				<!-- <value>myMethodInterceptor</value> -->
				<value>delegatingIntroductionInterceptor</value>
			</list>
		</property>
		<!-- 指定目標物件 -->
		<property name="target" ref="welcomeServiceTarget"></property>
	</bean>
ApplicationContext ac = new ClassPathXmlApplicationContext("study/spring/aop/dii.xml");
		WelcomeService ws = (WelcomeService)ac.getBean("welcomeService");
		ModifyData md = (ModifyData)ws;
		md.setData("data");
		System.out.println(md.getData());


相關推薦

spring 學習筆記 引入通知

建立引入引入是為需要方法的類新增屬性和方法。可以用一個已存在的類讓他實現另外的介面,維持另外的狀態(這也叫混合)。引入能夠動態的建立複合物件,提供了多型繼承的好處。 class AuditableIntroductionInterceptor extends Delegat

Spring學習筆記四(AOP中的通知引數和註解開發)

 1.前言 上一篇部落格介紹瞭如何通過AOP來切入我們想實現的公共性的功能,這篇部落格來講一下,當我們攔截到方法後,如何來獲取通知引數。這也是AOP的精髓所在,通過AOP可以實現偷樑換柱的功能。我

17.Spring學習筆記_返回通知&異常通知&環繞通知(by尚矽谷_佟剛)

返回通知 無論連線點是正常返回還是丟擲異常, 後置通知都會執行. 如果只想在連線點返回的時候記錄日誌, 應使用返回通知代替後置通知在返回通知中, 只要將 returning 屬性新增到 @After

Spring學習筆記之前置通知、後置通知

在學習之前先提一個概念。 AsprctJ:Java社群裡最完整最流行的AOP框架。在Spring2.0以上版本中,可以使用基於AspectJ註解或基於XML配置的AOP。 啟用AspectJ註解支援 要在 Spring 中宣告 AspectJ 切面,

spring學習筆記(5)屬性檔案引入使用

外部檔案引用 在配置資料庫引數或郵箱伺服器引數等靜態資源時,相對於直接將資訊寫入Spring的.xml,.java等配置檔案中,更好的做法是將配置資訊分類獨立地存放到外部屬性檔案中,然後在Spring配置檔案中引入使用。這種做法的好處是: 1. 提高配置資訊

spring學習筆記(21)程式設計式事務配置,service層概念引入

訪問資料庫事務匯入 public <E> E add(Object object) { return (E) getSessionFactory().openSession().save(object); } 通過直接開啟sess

spring學習筆記(22)聲明式事務配置,readOnly無效寫無異常

lin top post 處理 ast cannot pro ever 也不會 在上一節內容中。我們使用了編程式方法來配置事務,這種優點是我們對每一個方法的控制性非常強。比方我須要用到什麽事務,在什麽位置假設出現異常須要回滾等。能夠進行非常細粒度的配置。

Spring學習筆記(一)

分布式 ransac 解析 工具 files 分布 工廠 根據 使用 Spring學習筆記(一) 因為最近在看《Spring 技術內幕-深入解析Spring 架構與設計原理》,相當於做了個筆記,以及把自己理解的東西記錄下來。 Spring整體架構圖 1.Spring子項目

Spring 學習筆記(五)—— Bean之間的關系、作用域、自動裝配

mar byname pps etc 有時 sysman 對象實例 構造 encoding 繼承   Spring提供了配置信息的繼承機制,可以通過為<bean>元素指定parent值重用已有的<bean>元素的配置信息。 <?xml

Spring 學習筆記(七)—— 切入點表達式

service string 出現 targe || 參數 public 例如 語法   為了能夠靈活定義切入點位置,Spring AOP提供了多種切入點指示符。 execution———用來匹配執行方法的連接點   

spring學習筆記(3)——bean配置細節註意

collect 1.5 之前 ice ble person name return 引用 1. 一個bean引用另外一個bean 當Person類中有一個屬性是Car,那麽該如何配置呢 person: package com.zj.spring; public class

Spring學習筆記之BeanDefinition

spring bean definition 在Spring容器中,Bean的實例以BeanDefinition來表示的。一個BeanDefinition描述了一個Bean實例。本文出自 “十裏稻花香” 博客,請務必保留此出處http://5880861.blog.51cto.com/587086

Spring學習筆記之BeanFactory

spring bean factory BeanFactory是一個頂級接口,下面看下它是幹什麽的。這裏說了,它是訪問Spring Bean容器的根接口,是Bean容器的基本視圖。它的一些子接口,比如ListableBeanFactory和ConfigurableBeanFactory都有特別的而

Spring學習筆記之ApplicationContext

spring application context 對於一個Application提供配置的核心接口,在應用運行的時候它是只讀的。一個ApplicationContext提供以下能力:1、可以通過BeanFactory中的方法訪問應用組件2、可以加載文件資源3、可以給已經註冊的監聽器發送事件4、

Spring學習筆記之啟動

spring 原理 啟動分析今天,以ClassPathXmlApplicationContext為例來看一下,Spring啟動的時候都做了什麽重點看refresh()方法refresh()方法是在AbstractApplicationContext類中定義的ClassPathXmlApplicationCo

spring學習筆記四:spring常用註解總結

bean logs single 配置文件 屬性註入 ring 如果 let ons 使用spring的註解,需要在配置文件中配置組件掃描器,用於在指定的包中掃描註解 <context:component-scan base-package="xxx.xxx.xxx

spring學習筆記(一) Spring概述

數據庫 spring容器 oot 基礎知識 spa 遠程 組合 主動 拓展 博主Spring學習筆記整理大部分內容來自Spring實戰(第四版)這本書. 強烈建議新手購入或者需要電子書的留言. 在學習Spring之前,我們要了解這麽幾個問題:什麽是Spring?Sprin

(來換項目系列)Spring學習筆記(2)

servlet span on() 購物車 集成 因此 proxy center str 3.1.1 配置profile bean 使用@profile 註解 例如 @Configuration @Profile("dev") public class xxxConfig{

Spring學習筆記(1)

作用域 構造器 上下文 實例 pojo bean 不同的 宋體 單例 激發pojo的潛能,不會讓pojo類繼承實現或導入與Spring API相關的任何東西 那麽spring怎麽裝配pojo呢-------->依賴註入(最大好處:松耦合) 耦合具有兩面性: 一方面

Spring學習筆記01-HelloWorld

復雜 asp tro 企業 沒有 spring學習 它的 -h style Spring概述:         Spring框架是一款非常優秀的輕量級開源框架,對代碼沒有任何的入侵性。它的設計理念及源碼的思想對學習是非常裨益的,該框架在大多企業級應用的系統架構中都有使用