AOP全稱是:aspect-oriented programming,它是面向切面編號的思想核心,
AOP和OOP既面向對象的編程語言,不相沖突,它們是兩個相輔相成的設計模式型
AOP技術彌補了面向對象編程思想的不足,spring aop是實現aop的一種技術,srping aop是spring框架中某個子框架或者子功能所依賴的核心。
SPring的容器并不依賴于AOP
這意味著程序員可以自己選擇是否使用aop技術,aop提供強大的中間件解決方案,這使用spring ioc容器更加的完善
二、一些術語2.1、術語純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 AOP3.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>中
以注解的方式對Java普通類進行標注
新聞熱點
疑難解答