Spring框架實(shí)現(xiàn)AOP的兩種方式詳解
第一種AOP實(shí)現(xiàn)方式
AfterLog
package com.xxx.demo.service1;
import org.junit.After;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(
"執(zhí)行了"+method.getName()+"返回的結(jié)果:"+returnValue
);
}
}
Log
package com.xxx.demo.service1;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//前置通知
public class log implements MethodBeforeAdvice {
@Override
//method:要執(zhí)行的目標(biāo)對(duì)象的方法 args:參數(shù) target:目標(biāo)讀寫(xiě)
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被執(zhí)行了");
}
}
配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注冊(cè)bean-->
<bean id="userService" class="com.xxx.demo.service1.UserServicelmp"></bean>
<bean id="log" class="com.xxx.demo.service1.log"></bean>
<bean id="afterLog" class="com.xxx.demo.service1.AfterLog"></bean>
<!-- 配置aop:需要導(dǎo)入aop的約束-->
<aop:config>
<!-- 切入點(diǎn):expression:表達(dá)式,execution(要執(zhí)行的位置!* * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.xxx.demo.service1.UserServicelmp.*(..))"/>
<!-- 執(zhí)行環(huán)繞增加 把log的類添加到切入點(diǎn)里面-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
實(shí)例調(diào)用
package com.xxx.demo.service1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//動(dòng)態(tài)代理代理的是接口
UserService userService =(UserService) context.getBean("userService");
userService.add();
userService.delete();
userService.select();
userService.update();
}
}
定義接口
package com.xxx.demo.service1;
public class UserServicelmp implements UserService{
@Override
public void add() {
System.out.println("增加了一個(gè)用戶");
}
@Override
public void delete() {
System.out.println("刪除了一個(gè)用戶");
}
@Override
public void update() {
System.out.println("更新了一個(gè)用戶");
}
@Override
public void select() {
System.out.println("查詢了一個(gè)用戶");
}
}

第二種AOP實(shí)現(xiàn)方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注冊(cè)bean-->
<bean id="userService" class="com.xxx.demo.service1.UserServicelmp"></bean>
<bean id="log" class="com.xxx.demo.service1.log"></bean>
<bean id="afterLog" class="com.xxx.demo.service1.AfterLog"></bean>
<!-- 方式二:自定義類-->
<bean id="diy" class="com.xxx.demo.service1.DiyPointCut"></bean>
<aop:config>
<!-- 自定義切面 ref 要引用的類-->
<aop:aspect ref="diy">
<!-- 切入點(diǎn)-->
<aop:pointcut id="point" expression="execution(* com.xxx.demo.service1.UserServicelmp.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"></aop:before>
<aop:after method="after" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
</beans>

到此這篇關(guān)于Spring框架實(shí)現(xiàn)AOP的兩種方式詳解的文章就介紹到這了,更多相關(guān)Spring實(shí)現(xiàn)AOP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在 Spring Boot 中使用 @Autowired和 @Bean
本文通過(guò)一個(gè)示例演示了如何在SpringBoot中使用@Autowired和@Bean注解進(jìn)行依賴注入和Bean管理,示例中定義了一個(gè)Student類,并通過(guò)配置類TestConfig初始化Student對(duì)象,在測(cè)試類中,通過(guò)@Autowired注解自動(dòng)注入Student對(duì)象并輸出其屬性值,感興趣的朋友跟隨小編一起看看吧2025-02-02
IntelliJ IDEA中查看當(dāng)前類的所有繼承關(guān)系圖
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中查看當(dāng)前類的所有繼承關(guān)系圖,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
spring boot 測(cè)試單元修改數(shù)據(jù)庫(kù)不成功的解決
這篇文章主要介紹了spring boot 測(cè)試單元修改數(shù)據(jù)庫(kù)不成功的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot 2.0 mybatis mapper-locations掃描多個(gè)路徑的實(shí)現(xiàn)
這篇文章主要介紹了springboot 2.0 mybatis mapper-locations掃描多個(gè)路徑的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)現(xiàn)將CSV轉(zhuǎn)為Excel的示例代碼
CSV(Comma?Separated?Values)文件是一種純文本文件,包含用逗號(hào)分隔的數(shù)據(jù),常用于將數(shù)據(jù)從一個(gè)應(yīng)用程序?qū)牖驅(qū)С龅搅硪粋€(gè)應(yīng)用程序。本文將利用Java實(shí)現(xiàn)CSV轉(zhuǎn)為Excel,感興趣的可以了解一下2022-03-03
java實(shí)現(xiàn)微信點(diǎn)餐申請(qǐng)微信退款
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信點(diǎn)餐申請(qǐng)微信退款,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
解決idea中debug工具欄消失后如何顯示的問(wèn)題
這篇文章主要介紹了解決idea中debug工具欄消失后如何顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Springboot詳解RocketMQ實(shí)現(xiàn)消息發(fā)送與接收流程
這篇文章主要介紹了SpringBoot整合RocketMQ實(shí)現(xiàn)消息發(fā)送和接收功能,我們使用主流的SpringBoot框架整合RocketMQ來(lái)講解,使用方便快捷,本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

