第三章 AOP 基於Schema的AOP
基於Schema定義的切麵和前現兩種方式定義的切麵,內容上都差不多,隻是表現形式不一樣而已。
3.7.1一般增強的使用
a、目標類
public class Target { public void say(){ System.out.println("say..."); } public String getName(int id,String name){ System.out.println("getName..."); return "MR"+name+id; } public void around(){ System.out.println("around..."); } public void targetThrow(){ System.out.println("targetThrow"); throw new RuntimeException("我是一個運行期異常..."); } }
b、POJO(增強所在的類)
public class Pojo { public void before() { System.out.println("前置增強"); } public void afterReturning(String retName, int id, String name) { System.out.println("後置增強,返回值為:"+retName+" 入參為:"+id+"-"+name); } public Object around(ProceedingJoinPoint point) throws Throwable{ System.out.println("方法執行之前"); Object object = point.proceed(); System.out.println("方法執行之後"); return object; } public void throwEx(Exception ex){ System.out.println("拋出異常增強,異常信息:"+ex.getMessage()); } public void finalEx(){ System.out.println("Final增強"); } }
c、aop命名空間與Schema方式配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 目標類 --> <bean ></bean> <!-- 增強所在的類 --> <bean ></bean> <!-- 配置基於schema的切麵 --> <aop:config proxy-target-> <!-- 確定增強所在類,並引入 --> <aop:aspect ref="advisor"> <!— 前置增強 method是配置增強所在類中的方法 --> <aop:before method="before" pointcut="target(cn.framelife.spring.schema.Target)"/> <!— 後置增強 Returning 是返回值,必須和method中的參數名是一樣的 在Schema配置中,多個切點函數的與操作是and,或操作是or --> <aop:after-returning method="afterReturning" pointcut="execution(* cn.framelife.spring.schema..getName(..)) and args(id,name)" returning="retName" arg-names="retName,id,name"/> <!-- 環繞增強 --> <aop:around method="around" pointcut="execution(* cn.framelife.spring.schema..around(..))"/> <!— 拋出異常增強 throwing是異常對象,必須和method中的參數是一樣的 --> <aop:after-throwing method="throwEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))" throwing="ex"/> <!-- Final增強 --> <aop:after method="finalEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))"/> </aop:config> </beans>
d、測試
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); Target target = (Target) context.getBean("target"); target.say(); target.getName(10, "Zhang"); target.around(); target.targetThrow();
e、結果
前置增強 say... 前置增強 getName... 後置增強,返回值為:MRZhang10 入參為:10-Zhang 前置增強 方法執行之前 around... 方法執行之後 前置增強 targetThrow 拋出異常增強,異常信息:我是一個運行期異常... Final增強 Exception in thread "main" java.lang.RuntimeException: 我是一個運行期異常...
3.7.2引介增強的使用
我們還是使用3.6.2@DeclareParents中的例子:Waiter為目標類,然後讓目標類擁有ISeller接口的功能:
https://blog.csdn.net/p_3er/article/details/9269407
a、兩個接口與兩個類
目標類與其接口:
- public interface IWaiter {
- public void service();
- }
- @Component
- public class Waiter implements IWaiter {
- @Override
- public void service() {
- System.out.println("service");
- }
- }
運行期織入到目標類的功能類與其接口:
- public interface ISeller {
- public void sell();
- }
- public class Seller implements ISeller {
- @Override
- public void sell() {
- System.out.println("sell");
- }
- }
b、配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 目標類 --> <bean ></bean> <!-- 增強所在的類 --> <bean ></bean> <aop:config proxy-target-> <!— 雖然引介增強不需要在增強所在的類中定義一個方法用於增強的實現,但<aop:aspect ref="advisor">中的ref屬性依然要指定一個增強Bean --> <aop:aspect ref="advisor"> <!— 引介增強 types-matching 目標類 implement-interface 要織入目標類的接口 default-impl 織入接口的實現類 --> <aop:declare-parents types-matching="cn.framelife.spring.schema.IWaiter+" implement-interface="cn.framelife.spring.schema.ISeller" default-impl="cn.framelife.spring.schema.Seller"/> </aop:aspect> </aop:config> </beans>
c、測試
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); IWaiter waiter = (IWaiter) context.getBean("waiter"); waiter.service(); ISeller seller = (ISeller)waiter; seller.sell();
d、結果
service sell
最後更新:2017-04-03 16:48:31
上一篇:
boa服務器 說明
下一篇:
struts標簽格式化時間<s:date name="neowaveBloodPressure.monitorTime" format="yyyy-MM-dd HH:mm:ss&q
刪除一個目錄下部分類型之外的所有文件的三種方法
利用Doug Lea的並發包實現帶超時機製的線程池
數據顯示 Google Reader 流量仍然遠超 Google+
PostgreSQL 聚合函數講解 - 2 相關性統計
麵向對象中的重載、覆蓋和隱藏
晚綁定場景下對象屬性賦值和取值可以不需要PropertyInfo
android儀表統計柱狀等圖標的繪製(開源庫XCL-Charts)
Vmware、Virtuozoo、Virtual Server 、Xen四款虛擬機的性能比較
彭安傑:身份驗證和數據安全標準可嵌入物聯網設計裏
【雲棲精選7月刊】拋開晦澀的算法、模型,讓我們來談談互聯網架構