小編給大家分享一下spring AOP中Around如何增強實現(xiàn)方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都成都iso認證小微創(chuàng)業(yè)公司專業(yè)提供成都定制網(wǎng)站營銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。
一 配置
<?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包下所有類的、
// 所有方法的執(zhí)行作為切入點
@Around("execution(* org.crazyit.app.service.impl.*.*(..))")
public Object processTx(ProceedingJoinPoint jp)
throws java.lang.Throwable
{
System.out.println("執(zhí)行目標方法之前,模擬開始事務(wù)...");
// 獲取目標方法原始的調(diào)用參數(shù)
Object[] args = jp.getArgs();
if(args != null && args.length > 1)
{
// 修改目標方法的第一個參數(shù)
args[0] = "【增加的前綴】" + args[0];
}
// 以改變后的參數(shù)去執(zhí)行目標方法,并保存目標方法執(zhí)行后的返回值
Object rvt = jp.proceed(args);
System.out.println("執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...");
// 如果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 {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
void foo();
// 定義一個addUser()方法,模擬應(yīng)用中的添加用戶的方法
int addUser(String name, String pass);
}World
package org.crazyit.app.service;
public interface World {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar();
}四 實現(xiàn)類
HelloImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void foo() {
System.out.println("執(zhí)行Hello組件的foo()方法");
}
// 定義一個addUser()方法,模擬應(yīng)用中的添加用戶的方法
public int addUser(String name, String pass) {
System.out.println("執(zhí)行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 {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar() {
System.out.println("執(zhí)行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) {
// 創(chuàng)建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();
}
}六 測試結(jié)果
執(zhí)行目標方法之前,模擬開始事務(wù)...
執(zhí)行Hello組件的foo()方法
執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...
執(zhí)行目標方法之前,模擬開始事務(wù)...
執(zhí)行Hello組件的addUser添加用戶:【增加的前綴】孫悟空
執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...
addUser()的返回值為:400
執(zhí)行目標方法之前,模擬開始事務(wù)...
執(zhí)行World組件的bar()方法
執(zhí)行目標方法之后,模擬結(jié)束事務(wù)...
以上是“spring AOP中Around如何增強實現(xiàn)方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標題:springAOP中Around如何增強實現(xiàn)方法
URL鏈接:http://jinyejixie.com/article16/ghhpgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、小程序開發(fā)、微信小程序、營銷型網(wǎng)站建設(shè)、網(wǎng)站制作、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)