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)讀寫 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)文章
詳解Java如何實(shí)現(xiàn)基于Redis的分布式鎖
在不同進(jìn)程需要互斥地訪問共享資源時(shí),分布式鎖是一種非常有用的技術(shù)手段。這篇文章運(yùn)用圖文和實(shí)例代碼介紹了Java如何實(shí)現(xiàn)基于Redis的分布式鎖,文章介紹的很詳細(xì),對(duì)Java和Redis剛興趣的朋友們可以參考借鑒,下面來一起看看。2016-08-08spring?@Transactional注解中常用參數(shù)詳解
這篇文章主要介紹了spring?@Transactional注解中常用參數(shù)詳解,事物注解方式:?@Transactional,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02Java 獲取服務(wù)器環(huán)境的實(shí)例詳解
這篇文章主要介紹了Java 獲取服務(wù)器環(huán)境的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例和輸出結(jié)果,希望能幫助大家理解,需要的朋友可以參考下2017-07-07SpringBoot Web開發(fā)之系統(tǒng)任務(wù)啟動(dòng)與路徑映射和框架整合
這篇文章主要介紹了SpringBoot Web開發(fā)中的系統(tǒng)任務(wù)啟動(dòng)與路徑映射和Servlet、Filter、Listener框架整合,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Java集合和IO流實(shí)現(xiàn)水果攤項(xiàng)目
最近閑來無事,使用java基礎(chǔ)知識(shí)集合和IO流做了一個(gè)簡(jiǎn)單的小項(xiàng)目,水果攤項(xiàng)目,用到GUI和Mysql數(shù)據(jù)庫搭建,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-06-06Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳)
本篇文章主要介紹了Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java使用fill()數(shù)組填充的實(shí)現(xiàn)
這篇文章主要介紹了Java使用fill()數(shù)組填充的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Springboot基礎(chǔ)學(xué)習(xí)之初識(shí)SpringBoot
今天帶大家學(xué)習(xí)Springboot基礎(chǔ)知識(shí),文中有非常詳細(xì)的圖文解說及代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05