1. 程式人生 > >Spring AOP原理及攔截器

Spring AOP原理及攔截器

<?xml version="1.0" encoding="UTF-8"?> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). Jpetstore 的應用上下文定義,包含事務管理和引用了在 dataAccessContext-local/jta.xml(具體使用了哪個要看 web.xml 中的 'contextConfigLocation' 的配置)中註冊的DAO
--> <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"> <!-- ========================= GENERAL DEFINITIONS ========================= --> <!-- Configurer that replaces ${...} placeholders with values from properties files 佔位符的值將從列出的屬性檔案中抽取出來
--> <!-- (in this case, mail and JDBC related properties) --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>WEB-INF/mail.properties</value> <value>WEB-INF/jdbc.properties</value> </list> </property> </bean> <!-- MailSender used by EmailAdvice 指定用於傳送郵件的 javamail 實現者,這裡使用了 spring 自帶的實現。此 bean 將被 emailAdvice 使用
--> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}"/> </bean> <!-- ========================= BUSINESS OBJECT DEFINITIONS ======================== --> <!-- 不需要,因為被 SpringMVC 的實現使用 Generic validator for Account objects, to be used for example by the Spring web tier --> <bean id="accountValidator" class="org.springframework.samples.jpetstore.domain.logic.AccountValidator"/> <!-- 不需要,因為被 SpringMVC 的實現使用 Generic validator for Order objects, to be used for example by the Spring web tier --> <bean id="orderValidator" class="org.springframework.samples.jpetstore.domain.logic.OrderValidator"/> <!-- 主要的商業邏輯物件,即我們所說的門面物件
注入了所有的DAO,這些DAO是引用了 dataAccessContext-xxx.xml 中定義的DAO
門面物件中的所有方法的事務控制將通過下面的 aop:config 來加以控制 - JPetStore primary business object (default implementation). - Transaction advice gets applied through the AOP configuration below. --> <bean id="petStore" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl"> <property name="accountDao" ref="accountDao"/> <property name="categoryDao" ref="categoryDao"/> <property name="productDao" ref="productDao"/> <property name="itemDao" ref="itemDao"/> <property name="orderDao" ref="orderDao"/> </bean> <!-- ========================= ASPECT CONFIGURATION ======================== --> <!-- AOP配置,用來控制方法將需要進行事務處理,採用了AspectJ 的語法 --> <aop:config> <!-- This definition creates auto-proxy infrastructure based on the given pointcut, expressed in AspectJ pointcut language. Here: applying the advice named "txAdvice" to all methods on classes named PetStoreImpl. --> <!-- 指出在 PetStoreFacade 的所有方法都將採用 txAdvice(在緊接著的元素中定義了)事務方針,注意,我們這裡雖然指定的是介面 PetStoreFacace, 但其暗示著其所有的實現類也將
        同樣具有這種性質,因為本身就是實現類的方法在執行的,介面是沒有方法體的。 --> <aop:advisor pointcut="execution(* *..PetStoreFacade.*(..))" advice-ref="txAdvice"/> <!-- This definition creates auto-proxy infrastructure based on the given pointcut, expressed in AspectJ pointcut language. Here: applying the advice named "emailAdvice" to insertOrder(Order) method of PetStoreImpl --> <!-- 當執行 PetStoreFacade.insertOrder方法,該方法最後一個引數為Order型別時(其實我們的例子中只有一個 insertOrder 方法,但這告訴了我們,當我們的介面或類中有過載了的方法,
        並且各個過載的方法可能使用不同的攔截機機制時,我們可以通過方法的引數加以指定),將執行emailAdvice(在最後定義的那個元素)--> <aop:advisor pointcut="execution(* *..PetStoreFacade.insertOrder(*..Order))" advice-ref="emailAdvice"/> </aop:config> <!--       事務方針宣告,用於控制採用的事務策略 Transaction advice definition, based on method name patterns. Defaults to PROPAGATION_REQUIRED for all methods whose name starts with "insert" or "update", and to PROPAGATION_REQUIRED with read-only hint for all other methods. --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="insert*"/> <tx:method name="update*"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 攔截機,用於在適當的時機(通過AOP配置,如上面)在方法執行成功後傳送郵件 AOP advice used to send confirmation email after order has been submitted --> <!-- --> <bean id="emailAdvice" class="org.springframework.samples.jpetstore.domain.logic.SendOrderConfirmationEmailAdvice"> <property name="mailSender" ref="mailSender"/> </bean> <!-- ========================= 忽略 REMOTE EXPORTER DEFINITIONS ======================== --> </beans>