Spring 對 AOP 的支援,springaop支援
1、AOP概念
首先,AOP是對OOP的一個補充。它考慮的是“橫切性”問題。橫切性問題即可以理解為我們同一層類的問題(例如:Service層)。
它的理念是,把遍曆在系統各個角落具有橫切性的獨立的服務,抽出來放到一個地方,然後等到運行時,再放進去,考慮的是“橫向”的東西。將橫切性關注的東西給抽出來,會使代碼大大減少,更加簡潔,更加有複用性。
展示AOP中的基本概念:
基礎概念不多說,從可以看出SpringAOP的實現的過程:
Ø 找到橫切性性關注點(發現橫切性問題,具體實現為Advice)
Ø 編寫切入點(Pointcut),即指定Advice的方法的範圍
Ø 將Advice織入(Weave)目標對象(TargetObject)的Joinpoint
織入(Weave)是動態,預設採用的方式是JDK的動態代理,當然也可以採用CGLIB代理。
可簡單地表示為:
2、Spring 對 AOP 的支援—採用Annotation方式
依賴包配置:
*SPRING_HOME/dist/spring.jar
*SPRING_HOME/lib/log4j/log4j-1.2.14.jar
*SPRING_HOME/lib/jakarta-commons/commons-logging.jar
*SPRING_HOME/lib/aspectj/*.jar --(要使用aspectj提供的相關的註解)
源碼
interface UserManager
package com.bjpowernode.spring;public interface UserManager {public void addUser(String userName, String password);}
class UserManagerImpl
package com.bjpowernode.spring;public class UserManagerImpl implements UserManager {public void addUser(String username, String password) {System.out.println("---------UserManagerImpl.add()--------");}}
*class SecurityHandler(將橫切性關注點模組化,建立相關的類)
package com.bjpowernode.spring;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class SecurityHandler {/** * 定義Pointcut,Pointcut的名稱為addAddMethod(),此方法沒有傳回值和參數 該方法就是一個標識,不進行調用 */@Pointcut("execution(* add*(..))")private void addAddMethod() {};/** * 定義Advice,表示我們的Advice應用到哪些Pointcut訂閱的Joinpoint上 */@Before("addAddMethod()")// @After("addAddMethod()")private void checkSecurity() {System.out.println("");System.out.println("-------checkSecurity-------");}}
為所有的方法名中帶add的方法,執行之前都加入checkSecurity()服務。
Spring設定檔—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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 啟用AspectJ對Annotation的支援 --> <aop:aspectj-autoproxy/> <bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/> <bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/></beans>
classClient—用戶端
package com.bjpowernode.spring;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client {public static void main(String[] args) {BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.print(factory);UserManager userManager = (UserManager) factory.getBean("userManager");userManager.addUser("張三", "123");}}
運行結果:
-------checkSecurity----------------UserManagerImpl.add()--------
3、Spring 對 AOP 的支援—採用"設定檔"方式
與上面實現相同的效果。
* 依賴包配置採用上面的“採用Annotation方式”即可
* 直接看源碼:
interface UserManager與上面相同
class UserManagerImpl與上面相同
class SecurityHandler(將橫切性關注點模組化,建立相關的類):
package com.bjpowernode.spring;public class SecurityHandler {private void checkSecurity() {System.out.println("");System.out.println("-------checkSecurity-------");}}
*Spring設定檔—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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/><bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/><aop:config><aop:aspect id = "securityAspect" ref="securityHandler"><!-- 以Add為開頭的方法<aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> --><!-- com.bjpowernode.spring包下所有類的所有方法<aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.*(..))"/>--><!-- com.bjpowernode.spring包下所有類的所有add與del方法 --><aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.add*(..))||execution(* com.bjpowernode.spring.*.del*(..))"/><!-- Advice,配一個Pointcut --><aop:before method="checkSecurity" pointcut-ref="addAddMethod"/></aop:aspect></aop:config></beans>
主要是在aspect節中,將Advice與Poincut聯絡起來
classClient—用戶端也與上面相同
運行結果正確:
-------checkSecurity----------------UserManagerImpl.add()--------
4、總結
做AOP,重點是,發現Aspect(切面),並在“運行時”切入進去。織入過程,預設採用JDK的動態代理。動態靈活,但比較慢;靜態比較快(可以先行編譯),但不靈活。Spring的AOP可以針對pojo對象提供聲明式服務,如聲明式事務; Ejb裡面的SessionBean也可以提供聲明式事務。