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