小編給大家分享一下spring AOP中Around如何增強實現方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 指定自動搜索Bean組件、自動搜索切面類 --> <context:component-scan base-package="org.crazyit.app.service ,org.crazyit.app.aspect"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" /> </context:component-scan> <!-- 啟動@AspectJ支持 --> <aop:aspectj-autoproxy /> </beans>
二 切面類
package org.crazyit.app.aspect; import org.aspectj.lang.annotation.*; import org.aspectj.lang.*; // 定義一個切面 @Aspect public class TxAspect { // 匹配org.crazyit.app.service.impl包下所有類的、 // 所有方法的執行作為切入點 @Around("execution(* org.crazyit.app.service.impl.*.*(..))") public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable { System.out.println("執行目標方法之前,模擬開始事務..."); // 獲取目標方法原始的調用參數 Object[] args = jp.getArgs(); if(args != null && args.length > 1) { // 修改目標方法的第一個參數 args[0] = "【增加的前綴】" + args[0]; } // 以改變后的參數去執行目標方法,并保存目標方法執行后的返回值 Object rvt = jp.proceed(args); System.out.println("執行目標方法之后,模擬結束事務..."); // 如果rvt的類型是Integer,將rvt改為它的平方 if(rvt != null && rvt instanceof Integer) rvt = (Integer)rvt * (Integer)rvt; return rvt; } }
三 接口
Hello
package org.crazyit.app.service; public interface Hello { // 定義一個簡單方法,模擬應用中的業務邏輯方法 void foo(); // 定義一個addUser()方法,模擬應用中的添加用戶的方法 int addUser(String name, String pass); }
World
package org.crazyit.app.service; public interface World { // 定義一個簡單方法,模擬應用中的業務邏輯方法 public void bar(); }
四 實現類
HelloImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; @Component("hello") public class HelloImpl implements Hello { // 定義一個簡單方法,模擬應用中的業務邏輯方法 public void foo() { System.out.println("執行Hello組件的foo()方法"); } // 定義一個addUser()方法,模擬應用中的添加用戶的方法 public int addUser(String name, String pass) { System.out.println("執行Hello組件的addUser添加用戶:" + name); return 20; } }
WorldImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; @Component("world") public class WorldImpl implements World { // 定義一個簡單方法,模擬應用中的業務邏輯方法 public void bar() { System.out.println("執行World組件的bar()方法"); } }
五 測試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { // 創建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Hello hello = ctx.getBean("hello", Hello.class); hello.foo(); hello.addUser("孫悟空", "7788"); World world = ctx.getBean("world", World.class); world.bar(); } }
六 測試結果
執行目標方法之前,模擬開始事務...
執行Hello組件的foo()方法
執行目標方法之后,模擬結束事務...
執行目標方法之前,模擬開始事務...
執行Hello組件的addUser添加用戶:【增加的前綴】孫悟空
執行目標方法之后,模擬結束事務...
addUser()的返回值為:400
執行目標方法之前,模擬開始事務...
執行World組件的bar()方法
執行目標方法之后,模擬結束事務...
以上是“spring AOP中Around如何增強實現方法”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯網站建設公司行業資訊頻道!
另外有需要云服務器可以了解下創新互聯建站vcdvsql.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網頁名稱:springAOP中Around如何增強實現方法-創新互聯
本文來源:http://vcdvsql.cn/article4/ddjsie.html
成都網站建設公司_創新互聯,為您提供電子商務、移動網站建設、網站改版、品牌網站建設、網站設計、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯