Spring AOP中怎么定義AfterReturning,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一 配置
<?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 LogAspect
{
// 匹配org.crazyit.app.service.impl包下所有類的、
// 所有方法的執行作為切入點
@AfterReturning(returning="rvt"
, pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
// 聲明rvt時指定的類型會限制目標方法必須返回指定類型的值或沒有返回值
// 此處將rvt的類型聲明為Object,意味著對目標方法的返回值不加限制
public void log(Object rvt)
{
System.out.println("獲取目標方法返回值:" + rvt);
System.out.println("模擬記錄日志功能...");
}
}三 接口
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()方法
獲取目標方法返回值:null
模擬記錄日志功能...
執行Hello組件的addUser添加用戶:孫悟空
獲取目標方法返回值:20
模擬記錄日志功能...
執行World組件的bar()方法
獲取目標方法返回值:null
模擬記錄日志功能...
看完上述內容,你們掌握Spring AOP中怎么定義AfterReturning的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。