日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

中國最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2

wkspring教程

Spring 中基于 AOP 的 XML架構(gòu)

閱讀 (2178)

Spring 中基于 AOP 的 XML架構(gòu)

為了在本節(jié)的描述中使用 aop 命名空間標簽,你需要導(dǎo)入 spring-aop j架構(gòu),如下所述:

<?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.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <!-- bean definition & AOP specific configuration -->

</beans>

你還需要在你的應(yīng)用程序的 CLASSPATH 中使用以下 AspectJ 庫文件。這些庫文件在一個 AspectJ 裝置的 ‘lib’ 目錄中是可用的,否則你可以在 Internet 中下載它們。

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

 聲明一個 aspect

一個 aspect 是使用 元素聲明的,支持的 bean 是使用 ref 屬性引用的,如下所示:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>

這里,“aBean” 將被配置和依賴注入,就像前面的章節(jié)中你看到的其他的 Spring bean 一樣。

聲明一個切入點

一個切入點有助于確定使用不同建議執(zhí)行的感興趣的連接點(即方法)。在處理基于配置的 XML 架構(gòu)時,切入點將會按照如下所示定義:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
   <aop:pointcut id="businessService"
      expression="execution(* com.xyz.myapp.service.*.*(..))"/>
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>

下面的示例定義了一個名為 “businessService” 的切入點,該切入點將與 com.tutorialspoint 包下的 Student 類中的 getName() 方法相匹配:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
   <aop:pointcut id="businessService"
      expression="execution(* com.tutorialspoint.Student.getName(..))"/>
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>

聲明建議

你可以使用 <aop:{ADVICE NAME}> 元素在一個 中聲明五個建議中的任何一個,如下所示:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
      <aop:pointcut id="businessService"
         expression="execution(* com.xyz.myapp.service.*.*(..))"/>
      <!-- a before advice definition -->
      <aop:before pointcut-ref="businessService" 
         method="doRequiredTask"/>
      <!-- an after advice definition -->
      <aop:after pointcut-ref="businessService" 
         method="doRequiredTask"/>
      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref="businessService"
         returning="retVal"
         method="doRequiredTask"/>
      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref="businessService"
         throwing="ex"
         method="doRequiredTask"/>
      <!-- an around advice definition -->
      <aop:around pointcut-ref="businessService" 
         method="doRequiredTask"/>
   ...
   </aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>

你可以對不同的建議使用相同的 doRequiredTask 或者不同的方法。這些方法將會作為 aspect 模塊的一部分來定義。

基于 AOP 的 XML 架構(gòu)的示例

為了理解上面提到的基于 AOP 的 XML 架構(gòu)的概念,讓我們編寫一個示例,可以實現(xiàn)幾個建議。為了在我們的示例中使用幾個建議,讓我們使 Eclipse IDE 處于工作狀態(tài),然后按照如下步驟創(chuàng)建一個 Spring 應(yīng)用程序:

步驟 描述
1 創(chuàng)建一個名為 SpringExample 的項目,并且在所創(chuàng)建項目的 src 文件夾下創(chuàng)建一個名為 com.tutorialspoint 的包。
2 使用 Add External JARs 選項添加所需的 Spring 庫文件,就如在 Spring Hello World Example 章節(jié)中解釋的那樣。
3 在項目中添加 Spring AOP 指定的庫文件 aspectjrt.jar, aspectjweaver.jaraspectj.jar。
4 com.tutorialspoint 包下創(chuàng)建 Java 類 Logging, StudentMainApp。
5 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml
6 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并且按如下解釋的那樣運行應(yīng)用程序。

這里是 Logging.java 文件的內(nèi)容。這實際上是 aspect 模塊的一個示例,它定義了在各個點調(diào)用的方法。

package com.tutorialspoint;
public class Logging {
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }
   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }
   /**
    * This is the method which I would like to execute
    * if there is an exception raised.
    */
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}

下面是 Student.java 文件的內(nèi)容:

package com.tutorialspoint;
public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      System.out.println("Age : " + age );
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      System.out.println("Name : " + name );
      return name;
   }  
   public void printThrowException(){
       System.out.println("Exception raised");
       throw new IllegalArgumentException();
   }
}

下面是 MainApp.java 文件的內(nèi)容:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      Student student = (Student) context.getBean("student");
      student.getName();
      student.getAge();      
      student.printThrowException();
   }
}

下面是配置文件 Beans.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.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <aop:config>
      <aop:aspect id="log" ref="logging">
         <aop:pointcut id="selectAll" 
         expression="execution(* com.tutorialspoint.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>

   <!-- Definition for student bean -->
   <bean id="student" class="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.tutorialspoint.Logging"/> 

</beans>

一旦你已經(jīng)完成的創(chuàng)建了源文件和 bean 配置文件,讓我們運行一下應(yīng)用程序。如果你的應(yīng)用程序一切都正常的話,這將會輸出以下消息:

Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....
other exception content

讓我們來解釋一下上面定義的在 com.tutorialspoint 中 選擇所有方法的 。讓我們假設(shè)一下,你想要在一個特殊的方法之前或者之后執(zhí)行你的建議,你可以通過替換使用真實類和方法名稱的切入點定義中的星號(*)來定義你的切入點來縮短你的執(zhí)行。

<?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.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <aop:config>
   <aop:aspect id="log" ref="logging">
      <aop:pointcut id="selectAll" 
      expression="execution(* com.tutorialspoint.Student.getName(..))"/>
      <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
      <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
   </aop:aspect>
   </aop:config>

   <!-- Definition for student bean -->
   <bean id="student" class="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.tutorialspoint.Logging"/> 

</beans>

如果你想要執(zhí)行通過這些更改之后的示例應(yīng)用程序,這將會輸出以下消息:

Going to setup student profile.
Name : Zara
Student profile has been setup.
Age : 11
Exception raised
.....
other exception content
關(guān)閉
程序員人生
主站蜘蛛池模板: 青青草综合 | 天堂a√在线 | a在线天堂| 欧美日韩视频在线 | 欧美福利一区二区 | 91亚洲精品一区二区 | 国产99精品视频 | 日韩午夜在线视频 | 欧美一级电影 | 精品天堂| 91在线小视频 | 黄色网页免费看 | 综合久久亚洲 | 亚洲精品在线播放 | 国产呦精品一区二区三区网站 | 久久久久久久av | 亚洲精品乱码久久久久久蜜糖图片 | 国产精品av在线 | 91麻豆精品国产91久久久使用方法 | 国产成人精品免费视频大全最热 | av麻豆 | 国产一区不卡视频 | 黄网站在线免费看 | 午夜精品一区二区三区在线视 | 日韩欧美精品一区 | 亚洲精品美女 | 成人综合婷婷国产精品 | 91精品国产综合久久福利 | 国产丝袜一区二区三区免费视频 | 国产精品久久久久久久久久久久久 | 精品一区二区三区久久久 | 欧美日韩福利视频 | 午夜av影院 | 精品一区二区不卡 | 99久久久久国产精品免费 | 黄色伊人 | 成人做爰www免费看视频网战 | 99视频在线免费观看 | 精品久久久久久亚洲精品 | 嫩草影院2019 | 在线日韩 |