javascript
快速入门Spring之SpringAOP
學習目標
1、AOP簡介 2、AOP在Spring中的實現 3、AOP的注解配置 4、AOP日志跟蹤案例1、AOP簡介
1.1 AOP基本概念
AOP(Aspect Oriented Programming)面向切面編程,通過預編譯方式和運行期間動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容。
1.2 AOP的作用
AOP的核心作用是:在程序運行期間,不修改代碼的同時為程序增強功能。將必不可少的公共功能做成切面,隨著程序運行切入到代碼中運行。編寫業務時只關注于核心功能 ,不再考慮事務、日志等公共功能,減輕了編碼負擔,更專注于業務。
解耦,分離核心業務和非核心業務,非核心業務由AOP的切面實現,用戶只需要關注核心業務。
1.3 AOP的術語
1、切面(Aspect)
對哪些方法進行攔截,攔截后怎么處理,這些關注點稱之為切面
2、連接點(joinpoint)
被攔截到的點,因為Spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構造器
3、切入點(pointcut)
對連接點進行攔截的定義
4、通知(advice)
所謂通知指的就是指攔截到連接點之后要執行的代碼,通知分為前置、后置、異常、最終、環繞通知五類
5、目標對象(target)
代理的目標對象,將切面應用到目標對象并導致代理對象創建的過程
6、引入\織入(introduction、weave)
在不修改代碼的前提下,引入可以在運行期為類動態地添加一些方法或字段
# 2、AOP的實現
2.1 AOP配置步驟
2.1.1 引入依賴
<dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.8.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.5</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.5</version></dependency>2.1.2 編寫通知類
/*** 日志輸出通知類*/ public class LogAdvise {public void beforeLog(){System.out.println("方法開始執行!");}public void afterLog(){System.out.println("方法后置執行!");}public void afterReturning(){System.out.println("方法返回了數據");}public void afterThrowing(){System.out.println("方法拋出了異常");}public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("around方法名:" + joinPoint.getSignature().getName());System.out.println("around --前置");//原來方法joinPoint.proceed();System.out.println("around --后置");} }2.1.3 AOP的配置
<?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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置包的掃描--><context:component-scan base-package="com.blb.aop_demo"></context:component-scan><!--配置通知類--><bean id="logAdvise" class="com.blb.aop_demo.util.LogAdvise"></bean><!--配置切面--><aop:config><!--配置切入點--><aop:pointcut id="pc" expression="execution(* com.blb.aop_demo.service.*Service.*(..))"/><!--配置切面 ref是通知類的bean--><aop:aspect id="aspect1" ref="logAdvise"><!--前置通知 method是對應的通知方法 pointcut-ref是切入點--><aop:before method="beforeLog" pointcut-ref="pc"></aop:before><!--后置--><aop:after method="afterLog" pointcut-ref="pc"></aop:after><!--后置返回--><aop:after-returning method="afterReturning" pointcut-ref="pc"></aop:after-returning><!--后置拋異常--><aop:after-throwing method="afterThrowing" pointcut-ref="pc"></aop:after-throwing><!--環繞--><aop:around method="around" pointcut-ref="pc"></aop:around></aop:aspect></aop:config> </beans>2.1.4 測試
在com.blb.aop_demo.service包下添加幾個Service類做測試
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml"); GoodsService goodsService = context.getBean(GoodsService.class); goodsService.queryGoods(); goodsService.queryGoodsById(1); goodsService.createGoods(); goodsService.updateGoods(); goodsService.deleteGoodsById(1);2.1.5 配置詳解
aop:pointcut 是切入點配置
其中的核心是expression,通過表達式控制切面應用的范圍
語法:
execution(訪問修飾符 返回值 包名.類名.方法名(參數類型,參數類型....))通配符:
*代表任意長度的字符.. 代替子包或任意參數3、AOP的注解配置
3.1 常用AOP相關注解
@Aspect 切面,配置到切面類上
@PointCut(“表達式”) 配置切入點,加在方法上
@Before 配置前置通知方法
@After 配置后置通知方法
@Around 配置環繞通知方法
@AfterReturning 配置后置返回值通知方法
@AfterThrowing 配置后置拋出異常通知方法
3.2 AOP配置
1)配置類
@ComponentScan(basePackages = "com.blb.aop_demo") @Configuration //啟動AspectJ的注解配置 @EnableAspectJAutoProxy public class AopConfig { }2) 日志切面
/*** 日志切面*/ @Aspect @Component public class LogAspect {//配置切入點@Pointcut("execution(* com.blb.aop_demo.service.*Service.*(..))")public void pointcut(){}//配置通知方法@Before("pointcut()")public void beforeLog(){System.out.println("這是前置的通知方法!!");} }3)測試
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(AopConfig.class); GoodsService goodsService = context2.getBean(GoodsService.class); goodsService.queryGoods(); goodsService.queryGoodsById(1); goodsService.createGoods(); goodsService.updateGoods(); goodsService.deleteGoodsById(1);4、日志跟蹤案例
4.1 案例概述
在實際項目部署上線后,都需要通過日志的搜集來定位出現的bug,日志跟蹤代碼如果在所有方法都寫,就會很繁瑣,代碼也不利于維護,如果使用AOP就能很好解決這個問題。
4.2 案例實現
log4j slf4j logback …
導入log4j依賴
添加log4j.properties
編寫日志切面
作業
1) 給寵物管理系統的service實現類加入日志跟蹤,輸出執行方法名、參數、執行事件、返回值 (Log4j) AOP
2) 在上面的基礎上,區分日志
? 1) 查詢方法
? 輸出方法名,返回的結果
? 2) 增刪改方法
? 輸出方法名,參數,執行時間
面試題:
1) 簡介AOP
2) 介紹AOP的術語
3) 介紹AOP的XML配置方法
4) 介紹注解配置
總結
以上是生活随笔為你收集整理的快速入门Spring之SpringAOP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Spring入门学习笔记][Sprin
- 下一篇: SpringBoot + SpringS