【Spring-AOP-學習筆記-5】@AfterReturning增強處理簡單示例


項目結構




業務代碼


@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;
    }
}  

@Component("world")
public class WorldImpl implements World
{
    // 定義一個簡單方法,模擬應用中的業務邏輯方法
    public void bar()
    {
        System.out.println("執行World組件的bar()方法");
    }
}



定義切面Bean


@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("模擬記錄日志功能...");
    }
}  
說明:returing屬性所指定的形參名必須對應增強處理中的一個形參名,當目標方法執行返回后,返回值作為相應的參數值傳入增強處理方法中。
雖然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>  


測試代碼


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();
    }
} 

 

鏈接:
@AfterThrowing增強處理簡單示例 http://www.cnblogs.com/ssslinppp/p/4633595.html 
《@AfterReturning增強處理簡單示例》 http://www.cnblogs.com/ssslinppp/p/4633496.html 
《@After后向增強處理簡單示例》 http://www.cnblogs.com/ssslinppp/p/4633427.html 
《@Before前向增強處理簡單示例》  http://www.cnblogs.com/ssslinppp/default.html?page=7 
















附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM