AOP簡介以及Schema-based實作前置後置通知
又雙叒叕是程式汪2020-12-24 22:58
AOP簡介以及Schema-based實作前置後置通知
一、Aop--面向切面程式設計 英文(Aspect Oriented Programming)
正常程式都是縱向執行流程如下
而面向切面就意在切
又叫面向切面程式設計,在原有的縱向程式添加橫向程式切面
不需要修改原有程式代碼(展現出程式的高擴充性)
原有功能下相當于釋放了部分邏輯--讓職責更加精确
二、面向切面程式設計
在原有程式縱向執行流程執行流程中,針對某一些方法添加通知,形成橫切面的構成叫做面向切面程式設計。
常用概念
原有功能:切點,pointcut
前置通知: 在切點之前執行的功能,beforeadvice
後置通知:在切點之後之的功能,afteradvice
如果在切點執行構成中出現異常,會觸發異常通知。throws advice
所有的功能的總稱叫做切面。
織入:把切面嵌入到原有功能的過程叫做織入。
三、項目示範
第一步、導包
第二、Spring配置檔案的 編寫
Spring 官方注解幫助文檔
搜尋Spring xml注解
切面config
有參數切點
無參數切點
三、前置通知
作用:對主程式運作及其代碼沒有任何影響
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="mybefore" class="com.wq.advice.MyBeforeAdvice"></bean> <aop:config> <aop:pointcut expression="execution(* com.wq.test.Demo.Deom2())" id="mypoint"/><!-- 切點 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/> </aop:config> <bean id="demo" class="com.wq.test.Demo"></bean> </beans>
測試類
package com.wq.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) { ApplicationContext aContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Demo demo = aContext.getBean("demo",Demo.class); demo.Deom1(); demo.Deom2(); demo.Deom3(); }}
結果:
四、後置通知
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="mybefore" class="com.wq.advice.MyBeforeAdvice"></bean> <bean id="myafter" class="com.wq.advice.MyAfterAdvice"></bean> <aop:config> <aop:pointcut expression="execution(* com.wq.test.Demo.Deom2())" id="mypoint"/><!-- 切點 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/><!-- 形成前置通知 --> <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/><!--形成後置通知 --> </aop:config> <bean id="demo" class="com.wq.test.Demo"></bean> </beans>package com.wq.advice;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class MyAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { // TODO Auto-generated method stub System.out.println("執行後置通知"); }}
實作效果
注意:AOP對程式的高擴充 性
Spring提供了兩種AOP的實作方式
1.Schema-base
每個通知都需要實作接口或者類
配置Spring配置檔案時在<aop:config>配置
2.Aspectj
每個通知不需要實作接口或類
配置Spring配置檔案時是在<aop:config>的子标簽<aop:aspect>中配置
Schema-based(模式-基于模式)實作步驟總結
1.導入jar
2.建立通知類(前置,後置)可分别實作
3.Spring的配置檔案中配置Spring配置檔案
1.引入AOP命名空間
2.配置通知類Bean
3.配置切面
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置通知類的對象,在切面中需要引入 --> <bean id="mybefore" class="com.wq.advice.MyBeforeAdvice"></bean> <bean id="myafter" class="com.wq.advice.MyAfterAdvice"></bean> <!-- 配置切面 --> <aop:config> <!-- 配置切點 --> <aop:pointcut expression="execution(* com.wq.test.Demo.Deom2())" id="mypoint"/><!-- 切點 --> <!-- 通知 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/><!-- 形成前置通知 --> <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/><!--形成後置通知 --> </aop:config> <!--配置Demo類 --> <bean id="demo" class="com.wq.test.Demo"></bean> </beans>
4.編寫測試代碼
5.。運作結果