JavaWeb学习之路——SSM框架之Spring(六)
AOP:面向切面編程
1.介紹——AOP:面向切面編程。正常程序都是從上到下執(zhí)行相關(guān)代碼,aop是在縱向執(zhí)行流程中添加橫切面, 從左到右的關(guān)系。不需要修改成語原有代碼,它將代碼動(dòng)態(tài)的切入到類的指定方法、指定位置上的編程思想就是面向切面的編程。
特點(diǎn):高擴(kuò)展性、釋放原有功能部分邏輯
eg:一般程序執(zhí)行順序,從上往下。
public class Demo {public static void demo1() {System.out.println("demo1");}public static void demo2() {System.out.println("demo2");}public static void demo3() {System.out.println("demo3");}public static void main(String[] args) {demo1();demo2();demo3();} }2.概念
?
(1)原有功能:切點(diǎn),pointcut
(2)前置通知:在切點(diǎn)之前執(zhí)行的功能,before?advice
(3)后置通知:在切點(diǎn)之后執(zhí)行的功能,after?advice
(4)如果切點(diǎn)執(zhí)行過程中出現(xiàn)異常,會(huì)觸發(fā)異常通知,throws?advice
(5)所有功能總稱為切面
(6)織入:把切面嵌入到原有功能的過程
?
3.通知類型
(1)前置通知(Before advice):在某個(gè)連接點(diǎn)(Join point)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)的執(zhí)行(除非它拋出一個(gè)異常)。
(2)返回后通知(After returning advice):在某個(gè)連接點(diǎn)(Join point)正常完成后執(zhí)行的通知。例如,一個(gè)方法沒有拋出任何異常正常返回。
(3)拋出異常后通知(After throwing advice):在方法拋出異常后執(zhí)行的通知。
(4)后置通知(After(finally)advice):當(dāng)某個(gè)連接點(diǎn)(Join point)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是發(fā)生異常退出)。
(5)環(huán)繞通知(Around advice):包圍一個(gè)連接點(diǎn)(Join point)的通知,如方法調(diào)用。這是最強(qiáng)大的一種通知類型。環(huán)繞通知可以在方法前后完成自定義的行為。它也會(huì)選擇是否繼續(xù)執(zhí)行連接點(diǎn)或直接返回它們自己的返回值或拋出異常來結(jié)束行。
?
4.AOP實(shí)現(xiàn)切面過程
工程目錄圖:
(1)切入類的介紹:
public class Demo {public? void demo1() {System.out.println("demo1");}public? void demo2() {System.out.println("demo2");}public? void demo3() {System.out.println("demo3");}}(2)在環(huán)境xml中配置dtd說明文檔:
<?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/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">?
(3)對(duì)此對(duì)象的demo2()方法生成AOP切面:
<aop:config><aop:pointcut expression="excution(* com.likui.test.Demo.demo2())" id="mypoint"/></aop:config></beans>?
(4)形成前置和后置通知:新建前置通知類繼承前置接口
public class MyBeforeAdvice implements MethodBeforeAdvice{@Overridepublic void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {// TODO Auto-generated method stubSystem.out.println("執(zhí)行前置通知!");}}新建后置通知類繼承后置通知接口
public class MyAfterAdvice implements AfterReturningAdvice{@Overridepublic void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {// TODO Auto-generated method stubSystem.out.println("執(zhí)行后置通知!");}}?
(5)xml文件中成前置、后置和切入類對(duì)象,對(duì)于切入點(diǎn)進(jìn)行通知屬性配置:
<!-- 配置通知類對(duì)象 --><bean id="mybefore" class="com.likui.advice.MyBeforeAdvice"></bean><bean id="myafter" class="com.likui.advice.MyAfterAdvice"></bean><!-- 配置切面 --><aop:config><!-- 配置切點(diǎn) --><aop:pointcut expression="execution(* com.likui.test.Demo.demo2())" id="mypoint"/><!-- 配置通知 --><aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/><aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/></aop:config><!-- 配置類對(duì)象 --><bean id="demo" class="com.likui.test.Demo"></bean>?
(6)測(cè)試類編寫:
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");Demo demo=ac.getBean("demo",Demo.class);demo.demo1();demo.demo2();demo.demo3();}}(7)對(duì)Demo類中demo2()方法進(jìn)行前置和后置切面結(jié)果展示:
?
?
總結(jié)
以上是生活随笔為你收集整理的JavaWeb学习之路——SSM框架之Spring(六)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaWeb学习之路——SSM框架之S
- 下一篇: JavaWeb学习之路——SSM框架之S