国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring(五)AOP簡述

2019-11-14 23:41:56
字體:
來源:轉載
供稿:網友
SPRing(五)AOP簡述一、AOP簡述

AOP全稱是:aspect-oriented programming,它是面向切面編號的思想核心,

AOP和OOP既面向對象的編程語言,不相沖突,它們是兩個相輔相成的設計模式型

AOP技術彌補了面向對象編程思想的不足,spring aop是實現aop的一種技術,srping aop是spring框架中某個子框架或者子功能所依賴的核心。

SPring的容器并不依賴于AOP

這意味著程序員可以自己選擇是否使用aop技術,aop提供強大的中間件解決方案,這使用spring ioc容器更加的完善

二、一些術語2.1、術語
  • Cross-cutting concern:系統層面上的服務穿插到業務邏輯的處理流程之中
  • Aspect:當需要時,將其放到應用程序之上,不需要時,將其從應用程序中脫離出來
  • Advcie:是Aspect具體的實現,advice包括cross-cutting conerns的行為或者所提供的服務
  • Joinpoint:Aspect在應用程序執行時加入業務流程的時機
  • Pointcut:指定某個aspect在那些joinpoint時被穿插至應用程序之上
  • Target:一個advice被應用的對象或者目標對象
  • Instruction:為已經編寫,編譯完成的類,在執行時期動態的加入一些方法而不用修改或者增加任何代碼
  • Weave:被應用 到對象之上的過程

2.2、Spring對AOP的支持

java語言來編寫

定義pointcutes可以使用配置文件

不支持屬性成員的jointpoints.(spring 設計思想認為支持屬性成員的jointpoints會破壞對象的封裝性)

三、Spring創建Advice3.1、Before Advice

目標對象的方法執行之前被調用

通過實現MethodBeforeAdvice接口來實現

MethodBeforeAdvice 繼承自BeforeAdvice,而BeforeAdvice又繼承Advice接口,before方法會在目標對象target所指定的方法執行之前被調用執行。before返回值為void 沒有返回返回值,在before方法執行完成之后,才開始執行目標對象的方法.

3.2、實例
package com.pb;/** * 接口 * @author Administrator * */public interface IHello {    public void sayHello(String str);    }

package com.pb;/** * 接口實現類 * @author Administrator * */public class Hello implements IHello {    @Override    public void sayHello(String str) {        System.out.println("Hello    "+str);    }}

代理類

package com.pb;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;/** * 在方法執行前調用 * @author Administrator *實現了MethodBeforeAdvcie接口 */public class SayHelloBeforeAdvice implements MethodBeforeAdvice {    @Override    public void before(Method arg0, Object[] arg1, Object arg2)            throws Throwable {        System.out.println("====在方法執行前調用======");    }}

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 建立目標對象實例 --><bean id="hello" class="com.pb.Hello"/><!--  設定Advice實例--><bean id="sayBeforeAdvice" class="com.pb.SayHelloBeforeAdvice" /><!-- 建立代理對象 --><bean id="hellProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 設置代理接口 --><property name="proxyInterfaces"><value>com.pb.IHello</value></property><!--  設置目標對象實例--><property name="target"><ref local="hello"/></property><!--  設定Advice實例--><property name="interceptorNames"><list><value>sayBeforeAdvice</value></list></property></bean></beans>

測試類

package com.pb;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 測試類 * @author Administrator * */public class Test {    public static void main(String[] args) {        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        IHello iHello=(IHello) context.getBean("hellProxy");        iHello.sayHello("AOP");                    }}

結果:

====在方法執行前調用======Hello    AOP
3.3、After Advice

在目標對象方法執行之后被調用

通過實現AfterReturningAdvice接口來實現

在以上代碼上增加

package com.pb;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;/* * 在方法執行完后調用  * 實現 AfterRetruningAdvice接口 */public class SayAfterAdvice implements AfterReturningAdvice {    @Override    public void afterReturning(Object arg0, Method arg1, Object[] arg2,            Object arg3) throws Throwable {        System.out.println("=========在方法執行完后調用=======");    }}

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 建立目標對象實例 --><bean id="hello" class="com.pb.Hello"/><!--  設定beforAdvice實例--><bean id="sayBeforeAdvice" class="com.pb.SayHelloBeforeAdvice" /><!-- 設定AfterAdvice實例 --><bean id="sayAfterAdvice" class="com.pb.SayAfterAdvice" /><!-- 建立代理對象 --><bean id="hellProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 設置代理接口 --><property name="proxyInterfaces"><value>com.pb.IHello</value></property><!--  設置目標對象實例--><property name="target"><ref local="hello"/></property><!--  設定Advice實例--><property name="interceptorNames"><list><value>sayBeforeAdvice</value><value>sayAfterAdvice</value></list></property></bean></beans>

測試類不變,結果:

====在方法執行前調用======Hello    AOP=========在方法執行完后調用=======
3.4、Around Advice

在方法執行之間和之后來執行相應的操作

要實現接口MethodInterceptor接口

package com.pb;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class SayAroundAdvice implements MethodInterceptor {    @Override    public Object invoke(MethodInvocation arg0) throws Throwable {        System.out.println("=========在方法執行之前做點事情");        Object result=arg0.proceed();        System.out.println("在方法執行之后做點事情=========");        return result;    }    }

配置文件

<?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:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 建立目標對象實例 --><bean id="hello" class="com.pb.Hello"/><!--  設定beforAdvice實例--><bean id="sayBeforeAdvice" class="com.pb.SayHelloBeforeAdvice" /><!-- 設定AfterAdvice實例 --><bean id="sayAfterAdvice" class="com.pb.SayAfterAdvice" /><!-- 設定AroundAdvice實例 --><bean id="sayRoundAdvice" class="com.pb.SayAroundAdvice" /><!-- 建立代理對象 --><bean id="hellProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 設置代理接口 --><property name="proxyInterfaces"><value>com.pb.IHello</value></property><!--  設置目標對象實例--><property name="target"><ref local="hello"/></property><!--  設定Advice實例--><property name="interceptorNames"><list><!-- <value>sayBeforeAdvice</value><value>sayAfterAdvice</value> --><value>sayRoundAdvice</value></list></property></bean></beans>
3.5、THrowsAdvice

異常發生的時候,通知某個服務對象做處理

實現ThrowsAdvice接口

package com.pb;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;public class SayThowsAdvice implements ThrowsAdvice {        public void afterThrowing(Method method,Object[] objs,Object target,Throwable ta){        System.out.println("異常發生: "+ta+" 拋出異常的是: "+method);    }}

四、基于XML Schema

簡化代碼實現

容易對應程序進行維護

所有元素都定義在<aop:config>中

五、基于Annotation

以注解的方式對Java普通類進行標注


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四子王旗| 图片| 梓潼县| 定边县| 手游| 垫江县| 新河县| 庆云县| 宁蒗| 宁海县| 洛隆县| 石泉县| 德昌县| 镇康县| 广汉市| 治县。| 龙岩市| 阿克陶县| 泸定县| 阜阳市| 乃东县| 穆棱市| 铜川市| 九台市| 衡东县| 额尔古纳市| 平安县| 平果县| 潜江市| 桑日县| 亚东县| 南乐县| 资溪县| 金门县| 尚义县| 上蔡县| 仁布县| 兴宁市| 敦煌市| 轮台县| 中西区|