當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤
生活随笔
收集整理的這篇文章主要介紹了
Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.AOP?[知識點詳解]
AOP:中文名稱面向切面編程 英文名稱:(Aspect Oriented Programming) 正常程序執行流程都是縱向執行流程 3.1 又叫面向切面編程,在原有縱向執行流程中添加橫切面3.2 不需要修改原有程序代碼3.2.1 高擴展性3.2.2 原有功能相當于釋放了部分邏輯.讓職責更加明確.面向切面編程是什么? 4.1 在程序原有縱向執行流程中,針對某一個或某一些方法添加通知,形成橫切面過程就叫做面向切面編程.常用概念 5.1 原有功能: 切點, pointcut 5.2 前置通知: 在切點之前執行的功能. before advice5.3 后置通知: 在切點之后執行的功能, after advice5.4 如果切點執行過程中出現異常,會觸發異常通知.throws advice5.5 所有功能總稱叫做切面.5.6 織入: 把切面嵌入到原有功能的過程叫做織入5.6.1 spring 提供了 2 種AOP 實現方式5.6.2 Schema-based5.6.3 每個通知都需要實現接口或類5.6.4 配置spring 配置文件時在<aop:config>配置5.6.5 AspectJ5.6.6 每個通知不需要實現接口或類5.6.7 配置spring 配置文件是在<aop:config>的子標簽 ,<aop:aspect>中配置二.Schema-based 實現步驟
1.?導入jar
2.?新建通知類
1.1?新建前置通知類
1.1.1?arg0: 切點方法對象Method?對象
1.1.2?arg1: 切點方法參數
1.1.3 arg2:切點在哪個對象中
import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class MyBeforeAdvice implements MethodBeforeAdvice {@Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println("+++++輸出執行前置通知");} }? ? 2?新建后置通知類
2.1.1?arg0: 切點方法返回值
2.1.2?arg1:切點方法對象
2.1.3?arg2:切點方法參數
2.1.4?arg3:切點方法所在類的對象
public class MyAfterAdvice implements AfterReturningAdvice {@Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {System.out.println("執行后置通知"); } }3.配置spring?配置文件
1.1?引入aop?命名空間
1.2?配置通知類的<bean>
1.3?配置切面
1.4?*?通配符,匹配任意方法名,任意類名,任意一級包名
1.5 ?如果希望匹配任意方法參數 (..)
<?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/sc hema/beans http://www.springframework.org/schema/beans/spring-be ans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop. xsd"> <!-- 配置通知類對象,在切面中引入 --> <bean id="mybefore" class="com.bjsxt.advice.MyBeforeAdvice"></bean> <bean id="myafter" class="com.bjsxt.advice.MyAfterAdvice"></bean><!-- 配置切面 --> <aop:config> <!-- 配置切點 --> <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo2())" 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.bjsxt.test.Demo"></bean> </beans>4.編寫測試代碼
public class Test {public static void main(String[] args) {demo.demo3();ApplicationContext ac = newClassPathXmlApplicationContext("applicationContext.xm l");Demo demo = ac.getBean("demo",Demo.class);demo.demo1();demo.demo2();demo.demo3();} }
運行結果:
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/zhazhaacmer/p/10101772.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 团队作业9——第二次项目冲刺2(Beta
- 下一篇: gradle idea java ssm