當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring学习12之AOP2
生活随笔
收集整理的這篇文章主要介紹了
Spring学习12之AOP2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、AOP是什么?
AOP(Aspect Oriented Programming),即面向切面編程,可以說是OOP(Object Oriented Programming,面向對象編程)的補充和完善。
AOP核心概念
- 橫切關注點:跨越應用程序多個模塊的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日志,安全,緩存,事務等等…
- 切面(ASPECT):橫切關注點被模塊化的特殊對象。即,它是一個類。
- 通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
- 目標(Target):被通知對象。
- 代理(Proxy) :向目標對象應用通知之后創建的對象。
- 切入點(Pointcut) :切面通知執行的“地點"的定義
- 連接點(JointPoint):與切入點匹配的執行點。
二、代碼
1.需要的類
業務的接口
package com.shan.demo04;public interface UserService {void add();void delete();void update();void query();}業務的實現類
package com.shan.demo02; //改變業務代碼在公司是大忌,因為你改動代碼可能讓整個項目崩潰 public class UserServiceImpl implements UserService{@Overridepublic void add() {System.out.println("增加了一個用戶");}@Overridepublic void delete() {System.out.println("刪除了一個用戶");}@Overridepublic void update() {System.out.println("修改了一個用戶");}@Overridepublic void query() {System.out.println("查詢了一個用戶");} }前置日志類
package com.shan.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class BeforeLog implements MethodBeforeAdvice {//method:要執行的目標對象的方法// Object[]:參數// target:目標對象public void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了!");} }后置日志類
package com.shan.log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {//returnValue:返回值//method:要執行的目標對象的方法// Object[]:參數// target:目標對象public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("執行了!"+target.getClass().getName()+"的"+method.getName()+"方法.返回結果為:"+returnValue);} }自定義切入點
package com.shan.diy;public class DiyPointCut {public void before(){System.out.println("=================方法執行前==================");}public void after(){System.out.println("=================方法執行后==================");} }注解實現AOP
package com.shan.diy;import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;@Aspect //標注這個類是一個切面 public class AnnotationPointCut {@Before("execution(* com.shan.service.UserServiceImpl.*(..))")public void before(){System.out.println("=====方法執行前=====");}@After("execution(* com.shan.service.UserServiceImpl.*(..))")public void after(){System.out.println("=====方法執行后=====");}}Spring xml配置文件
<?xml version="1.0" encoding="UTF8" ?> <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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--注冊bean--><bean id="userService" class="com.shan.service.UserServiceImpl"/><bean id="beforeLog" class="com.shan.log.BeforeLog"/><bean id="afterLog" class="com.shan.log.AfterLog"/><!--配置aop方式2,自定義類--><bean id="diy" class="com.shan.diy.DiyPointCut"/><!--3,注解--><bean id="annotation" class="com.shan.diy.AnnotationPointCut"/><!--方式3,開啟注解支持--><aop:aspectj-autoproxy/><!--配置aop方式2,自定義類--><aop:config><!--自定義切面,ref:要引用的類--><aop:aspect ref="diy"><!--切入點--><aop:pointcut id="point" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/><aop:before method="before" pointcut-ref="point"/><aop:after method="after" pointcut-ref="point"/></aop:aspect></aop:config><aop:config><!--切入點--><aop:pointcut id="pointcut" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/><!--執行環繞增加--><aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></aop:config></beans>2.測試
import com.shan.service.UserService; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {@Testpublic void diyPointCutTest(){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//動態代理 代理的是接口UserService userService = context.getBean("userService", UserService.class);userService.add();}@Testpublic void annotationPointCutTest(){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//動態代理 代理的是接口UserService userService = context.getBean("userService",UserService.class);userService.add();} } > =================方法執行前================== > com.shan.service.UserServiceImpl的add被執行了! > =====方法執行前===== > 增加了一個用戶 > =====方法執行后===== > 執行了!com.shan.service.UserServiceImpl的add方法.返回結果為:null > =================方法執行后==================總結
AOP的實現方式:
- 默認的執行環繞
- 通過AOPconfig配置自定義的切入面,切入點,引用的類
- 注解實現AOP
對比有優先級,哪個config在上面,before就最先執行,after就最后執行.這兩種方式可以同時存在,個人發現對比有不同!
作者有話說
博客創作不易,希望看到這里的讀者動動你的小手點個贊,如果喜歡的小伙伴可以一鍵三連,作者大大在這里給大家謝謝了。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Spring学习12之AOP2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring学习10之动态代理
- 下一篇: Spring学习12之整合Mybatis