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)搜索切面類(lèi) -->
<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>
二 切面類(lèi)
package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定義一個(gè)切面
@Aspect
public class LogAspect
{
// 匹配org.crazyit.app.service.impl包下所有類(lèi)的、
// 所有方法的執(zhí)行作為切入點(diǎn)
@AfterReturning(returning="rvt"
, pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
// 聲明rvt時(shí)指定的類(lèi)型會(huì)限制目標(biāo)方法必須返回指定類(lèi)型的值或沒(méi)有返回值
// 此處將rvt的類(lèi)型聲明為Object,意味著對(duì)目標(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è)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
void foo();
// 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶(hù)的方法
int addUser(String name, String pass);
}
World
package org.crazyit.app.service;
public interface World {
// 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar();
}
四 實(shí)現(xiàn)類(lèi)
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è)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void foo() {
System.out.println("執(zhí)行Hello組件的foo()方法");
}
// 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶(hù)的方法
public int addUser(String name, String pass) {
System.out.println("執(zhí)行Hello組件的addUser添加用戶(hù):" + 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è)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar() {
System.out.println("執(zhí)行World組件的bar()方法");
}
}
五 測(cè)試類(lèi)
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();
}
}
六 測(cè)試結(jié)果
執(zhí)行Hello組件的foo()方法
獲取目標(biāo)方法返回值:null
模擬記錄日志功能...
執(zhí)行Hello組件的addUser添加用戶(hù):孫悟空
獲取目標(biāo)方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法
獲取目標(biāo)方法返回值:null
模擬記錄日志功能...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring通過(guò)Java配置集成Tomcat的方法
這篇文章主要介紹了Spring通過(guò)Java配置集成Tomcat的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Java.SE數(shù)組的一些常見(jiàn)練習(xí)題
數(shù)組可以看成是相同類(lèi)型元素的一個(gè)集合,在內(nèi)存中是一段連續(xù)的空間,這篇文章主要給大家介紹了關(guān)于Java.SE數(shù)組的一些常見(jiàn)練習(xí)題,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
JavaWeb頁(yè)面中防止點(diǎn)擊Backspace網(wǎng)頁(yè)后退情況
當(dāng)鍵盤(pán)敲下后退鍵(Backspace)后怎么防止網(wǎng)頁(yè)后退情況呢?今天小編通過(guò)本文給大家詳細(xì)介紹下,感興趣的朋友一起看看吧2016-11-11
Spring中@EnableScheduling實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例
這篇文章主要介紹了Spring中@EnableScheduling實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例,@EnableScheduling 注解開(kāi)啟定時(shí)任務(wù)功能,可以將多個(gè)方法寫(xiě)在一個(gè)類(lèi),也可以分多個(gè)類(lèi)寫(xiě),當(dāng)然也可以將方法直接寫(xiě)在上面ScheddulConfig類(lèi)中,需要的朋友可以參考下2024-01-01
Spring實(shí)戰(zhàn)之Bean銷(xiāo)毀之前的行為操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean銷(xiāo)毀之前的行為操作,結(jié)合實(shí)例形式分析了spring在bean銷(xiāo)毀之前的行為相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2019-11-11
SpringBoot中JPA實(shí)現(xiàn)Sort排序的三種方式小結(jié)
這篇文章主要介紹了SpringBoot中JPA實(shí)現(xiàn)Sort排序的三種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java并發(fā)編程中使用Executors類(lèi)創(chuàng)建和管理線(xiàn)程的用法
這篇文章主要介紹了Java并發(fā)編程中使用Executors類(lèi)創(chuàng)建和管理線(xiàn)程的用法,文中舉了用其啟動(dòng)線(xiàn)程和設(shè)置線(xiàn)程優(yōu)先級(jí)的例子,需要的朋友可以參考下2016-03-03

