Spring AOP定義AfterReturning增加實(shí)例分析
本文實(shí)例講述了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">
<!-- 指定自動(dòng)搜索Bean組件、自動(dòng)搜索切面類 -->
<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>
<!-- 啟動(dòng)@AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
二 切面類
package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定義一個(gè)切面
@Aspect
public class LogAspect
{
// 匹配org.crazyit.app.service.impl包下所有類的、
// 所有方法的執(zhí)行作為切入點(diǎn)
@AfterReturning(returning="rvt"
, pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
// 聲明rvt時(shí)指定的類型會(huì)限制目標(biāo)方法必須返回指定類型的值或沒有返回值
// 此處將rvt的類型聲明為Object,意味著對目標(biāo)方法的返回值不加限制
public void log(Object rvt)
{
System.out.println("獲取目標(biāo)方法返回值:" + rvt);
System.out.println("模擬記錄日志功能...");
}
}
三 接口
Hello
package org.crazyit.app.service;
public interface Hello {
// 定義一個(gè)簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
void foo();
// 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶的方法
int addUser(String name, String pass);
}
World
package org.crazyit.app.service;
public interface World {
// 定義一個(gè)簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar();
}
四 實(shí)現(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 {
// 定義一個(gè)簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void foo() {
System.out.println("執(zhí)行Hello組件的foo()方法");
}
// 定義一個(gè)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 {
// 定義一個(gè)簡單方法,模擬應(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í)行Hello組件的foo()方法
獲取目標(biāo)方法返回值:null
模擬記錄日志功能...
執(zhí)行Hello組件的addUser添加用戶:孫悟空
獲取目標(biāo)方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法
獲取目標(biāo)方法返回值:null
模擬記錄日志功能...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
欄 目:Java
下一篇:Java Linkedlist原理及實(shí)例詳解
本文標(biāo)題:Spring AOP定義AfterReturning增加實(shí)例分析
本文地址:http://www.jygsgssxh.com/a1/Java/8747.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過程代碼實(shí)例
- 01-10springmvc級聯(lián)屬性處理無法轉(zhuǎn)換異常問題解決
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
- 01-10Spring注解和同步鎖不能同步問題解決


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


