Spring AOP Schema aop:config、tx:advice

leiOOlei發表於2014-05-05

Spring AOP Schema  aop:config、tx:advice

一、      利用aop:config標籤實現AOP

首先看個例子,如下

介面程式碼:

package com.lei.demo.aop.schema;

public interface IHello {
    public void sayHello();
}

 

介面實現:

package com.lei.demo.aop.schema;

public class HelloService implements IHello {

    public void sayHello() {
        System.out.println("-----Hello World!-----");
    }

}

接下來我們要實現AOP,即呼叫sayHello方法時切入通知。

1.      第一種方法aop:config中配置aop:pointcut和aop:aspect

定義一個切面支援類HelloAspect.java

package com.lei.demo.aop.schema;

/*
 * 切面支援類
 */
public class HelloAspect {

    //前置通知  
    public void beforeAdvice() {  
        System.out.println("===========before advice");  
    }
    
    //後置最終通知  
    public void afterFinallyAdvice() {  
        System.out.println("===========after finally advice");
    }
}

 

Xml配置:Spring-AOP-Schema.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"  
        xsi:schemaLocation="  
           http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
           

    <!-- 目標類 -->
    <bean id="helloService" class="com.lei.demo.aop.schema.HelloService" />
    
    <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" />
    
    
    <!-- 配置切面 -->
    <!-- aop:advisor,是有順序的,必須放在aop:pointcut之後 -->
    <aop:config>
        <aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
        
        <aop:aspect ref="helloAspect">
            <!—以下使用了兩種方法定義切入點  pointcut-ref和pointcut-->
            <aop:before pointcut-ref="helloPointcut" method="beforeAdvice" />
            <aop:after pointcut="execution(* com.lei.demo.aop.schema..*.*(..))" method="afterFinallyAdvice" />
        </aop:aspect>
        
        
    </aop:config>
</beans>

以上配置中method="beforeAdvice"method="afterFinallyAdvice"對應helloAspect中的方法

 

測試,App.java

package com.lei.demo.aop.schema;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    private static ApplicationContext ctx;

    public static void main(String[] args) {
        
        ctx = new ClassPathXmlApplicationContext("Spring-AOP-Schema.xml");
        
        IHello helloService = ctx.getBean("helloService",IHello.class);
        
        helloService.sayHello();
    }

}

 

執行App.java,結果如下:

===========before advice

-----Hello World!-----

===========after finally advice

 

2.      第二種方法aop:config中配置aop:pointcut和aop:advisor

實現org.aopalliance.intercept.MethodInvocation介面,

HelloAroundAdvice.java如下:

package com.lei.demo.aop.schema;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HelloAroundAdvice implements MethodInterceptor {

    public Object invoke(MethodInvocation arg0) throws Throwable {

        System.out.println("++++++before advice");
        arg0.proceed();
        System.out.println("++++++after advice");
        
        return null;
    }

}

 

Xml配置:Spring-AOP-Schema.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"  
        xsi:schemaLocation="  
           http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop  
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
           
    <!-- 目標類 -->
    <bean id="helloService" class="com.lei.demo.aop.schema.HelloService" />
    
    <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" />
    
    <bean id="helloArroundAdvice" class="com.lei.demo.aop.schema.HelloAroundAdvice" />
    
    <!-- 配置切面 -->
    <aop:config>
        <aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
        <aop:advisor advice-ref="helloArroundAdvice" pointcut-ref="helloPointcut"/>
        
    </aop:config>
</beans>

 

仍然執行App.java,結果如下:

++++++before advice

-----Hello World!-----

++++++after advice

 

 

二、      AOP中加入事務,引入tx:advice

整合Hibernate時,需要引入事務,以下配置引用Spring 4.0.3和Hibernate 4.3.5

Spring-hibernate.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        ">

    <!-- Hibernate4 -->
    <!-- 載入資原始檔  其中包含變數資訊,必須在Spring配置檔案的最前面載入,即第一個載入-->
    <context:property-placeholder location="classpath:persistence-mysql.properties" />
    
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <!-- 可以加多個包 -->
                <value>com.lei.demo.entity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>
    
    <!-- 資料庫對映 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.user}" />
      <property name="password" value="${jdbc.pass}" />
   </bean>
   
    <!-- 配置Hibernate事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>
   
   <!-- 配置事務異常封裝 -->
   <bean id="persistenceExceptionTranslationPostProcessor" 
       class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
   
   <!--  宣告式容器事務管理 ,transaction-manager指定事務管理器為transactionManager-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config expose-proxy="true">
        <!-- 只對業務邏輯層實施事務 -->
        <aop:pointcut id="txPointcut" expression="execution(* com.lei.demo.service..*.*(..))" />
        <!-- Advisor定義,切入點和通知分別為txPointcut、txAdvice -->
        <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
        
    </aop:config>
    
</beans>

 

重點看一下tx:advice的配置

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="REQUIRED" />
        <tx:method name="*" read-only="true" />
    </tx:attributes>
</tx:advice>

意思是說,這個事務advice的管理者為transactionManager,而這個advice中規定了匹配add*get*方法的傳輸方式為required,也就是說沒有sessionFactory的時候會自動建立,有的時候就不會建立了。當然這只是規定了一個advice,你還需要指定一個aop:pointcut去引用它,例如下邊的aop:config配置節。這樣,這個advice就被織入到txPointcut這個pointcut上了。