spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法分析
本文實(shí)例講述了spring AOP的Around增強(qiáng)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一 配置
<?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 TxAspect
{
// 匹配org.crazyit.app.service.impl包下所有類的、
// 所有方法的執(zhí)行作為切入點(diǎn)
@Around("execution(* org.crazyit.app.service.impl.*.*(..))")
public Object processTx(ProceedingJoinPoint jp)
throws java.lang.Throwable
{
System.out.println("執(zhí)行目標(biāo)方法之前,模擬開始事務(wù)...");
// 獲取目標(biāo)方法原始的調(diào)用參數(shù)
Object[] args = jp.getArgs();
if(args != null && args.length > 1)
{
// 修改目標(biāo)方法的第一個(gè)參數(shù)
args[0] = "【增加的前綴】" + args[0];
}
// 以改變后的參數(shù)去執(zhí)行目標(biāo)方法,并保存目標(biāo)方法執(zhí)行后的返回值
Object rvt = jp.proceed(args);
System.out.println("執(zhí)行目標(biāo)方法之后,模擬結(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 {
// 定義一個(gè)簡(jiǎn)單方法,模擬應(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è)簡(jiǎn)單方法,模擬應(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è)簡(jiǎn)單方法,模擬應(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è)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar() {
System.out.println("執(zhí)行World組件的bar()方法");
}
}
五 測(cè)試類
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í)行目標(biāo)方法之前,模擬開始事務(wù)...
執(zhí)行Hello組件的foo()方法
執(zhí)行目標(biāo)方法之后,模擬結(jié)束事務(wù)...
執(zhí)行目標(biāo)方法之前,模擬開始事務(wù)...
執(zhí)行Hello組件的addUser添加用戶:【增加的前綴】孫悟空
執(zhí)行目標(biāo)方法之后,模擬結(jié)束事務(wù)...
addUser()的返回值為:400
執(zhí)行目標(biāo)方法之前,模擬開始事務(wù)...
執(zhí)行World組件的bar()方法
執(zhí)行目標(biāo)方法之后,模擬結(jié)束事務(wù)...
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring Boot詳解各類請(qǐng)求和響應(yīng)的處理方法
平時(shí)只是在用SpringBoot框架,但并沒有詳細(xì)研究過請(qǐng)求和響應(yīng)執(zhí)行的一個(gè)具體過程,所以本文主要來梳理一下SpringBoot請(qǐng)求和響應(yīng)的處理過程2022-07-07
Spring Task定時(shí)任務(wù)的配置和使用詳解
本篇文章主要介紹了Spring Task定時(shí)任務(wù)的配置和使用詳解,實(shí)例分析了Spring Task定時(shí)任務(wù)的配置和使用的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04
Mybatis-plus一對(duì)多分頁數(shù)據(jù)條數(shù)不正確的處理
這篇文章主要介紹了Mybatis-plus一對(duì)多分頁數(shù)據(jù)條數(shù)不正確的處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法
本文主要介紹了java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
spring-boot-maven-plugin未指定版本導(dǎo)致的編譯錯(cuò)誤問題
這篇文章主要介紹了spring-boot-maven-plugin未指定版本導(dǎo)致的編譯錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
java獲取百度網(wǎng)盤真實(shí)下載鏈接的方法
這篇文章主要介紹了java獲取百度網(wǎng)盤真實(shí)下載鏈接的方法,涉及java針對(duì)URL操作及頁面分析的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
關(guān)于yml文件字符串,List,Map的書寫方式并使用@ConfigurationProperties注入配置類
這篇文章主要介紹了關(guān)于yml文件字符串,List,Map的書寫方式并使用@ConfigurationProperties注入配置類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring Boot中如何使用Convert接口實(shí)現(xiàn)類型轉(zhuǎn)換器
這篇文章主要介紹了Spring Boot中使用Convert接口實(shí)現(xiàn)類型轉(zhuǎn)換器的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

