Spring 中基於 AOP 的 XML架構的一個實例

Spring 中基於 AOP 的 XML架構,我們來做第一個AOP實例

1.第一步:創建一個新項目,首先導入spring 所需的核心jar和AOP的所需要jar。 我這裏已經打包好了都在這個鏈接裏面

   spring 所需的核心jar和AOP的所需要jar: https://www.cnblogs.com/ysource/p/12855333.html

   Spring  AOP實例: https://www.cnblogs.com/ysource/p/12855333.html

  在項目下創建一個lib文件夾,把jar包複製到這個文件夾中。——》選擇jar包Build path,把jar包環境加載到java虛擬機中去 

如圖:

    

2.第二步:定義三個類

  2.1第一個Student類(包含學生姓名name,和學生年齡age)

1 package com.spring.aop1;
 2 
 3 public class Student {
 4     /**
 5      * 定義學生類
 6      */
 7     private String name;
 8     private Integer age;
 9 
10     public String getName() {
11         return name;
12     }
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18     public Integer getAge() {
19         return age;
20     }
21 
22     public void setAge(Integer age) {
23         this.age = age;
24     }
25 
26     public void printAdvice() {
27         System.out.println("name:" + name + ",age:" + age);
28 
29     }
30 
31 }

  2.2第二個類SeizedAdvice是我們要定義通知方法的類

1 package com.spring.aop1;
 2 
 3 public class SeizedAdvice {
 4     /**
 5      * 定義通知
 6      */
 7 
 8     public void beforeAdvice(){
 9         System.out.println("——————我是前置通知————————");
10         
11     }
12     public void afterAdvice(){
13         System.out.println("------我是後置通知-------");
14         
15     }
16     public void afterReturningAdvice(Object object){
17         System.out.println("————————我是返回後通知——————————"+object.toString());
18         
19     }
20     public void afterThrowingAdvice(IllegalAccessError illegalAccessError){
21         System.out.println("--------我是異常返回異常返回通知---------"+illegalAccessError.toString());
22     
23         
24     }
25     
26     
27     
28 }

 

  2.3第三個類就是測試類main

 1 package com.spring.aop1;
 2 
 3 
 4 
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 7 import org.springframework.context.support.AbstractApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class Main {
11     /**
12      * author:
13      * mail:[email protected]
14      * 時間:
15      */
16 
17     public static void main(String[] args) {
18         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/springAop.xml");
19         Student student=(Student)applicationContext.getBean("Student");
20         student.printAdvice();
21     }
22 
23 }

3.第三步:配置spring.xml文件,注意頭文件跟之前spring的頭文件有些區別,複製我這個就可以了。這裏的其他通知方法我註釋了,只留前置通知

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

    <!-- 配置aop代理 -->
    <aop:config>
        <!-- 配置切面, 通常是一個類,裏面可以定義切入點和通知 id:取名 ref:關聯 到那個bean -->
        <aop:aspect id="myAspect" ref="SeizedAdvice">
            <!-- 定義一個切入點 並給切入點起名爲myPointCut execution:是方法運行;第一個*代表作用域修飾符,如public,也可以不寫,用*代表任意值; 
                com.spring.aop1.*.*(..)代表着aop1包下的任意類、任意方法、任意參數個數 通俗的就是在哪些目標中做什麼 -->
            <aop:pointcut expression="execution(* com.spring.aop1.Student.printAdvice(..))"
                id="myPointCut" />
            <!-- pointcut-ref:指定切點通俗點就是在這類中將要做些什麼 method:方法 -->

            <aop:before pointcut-ref="myPointCut" method="beforeAdvice" />
            <!-- <aop:after pointcut-ref="SeizedAdvice" method="afterAdvice" />
            <aop:after-returning pointcut-ref="SeizedAdvice"
                returning="object" method="afterReturningAdvice" />
            <aop:after-throwing pointcut-ref="SeizedAdvice"
                throwing="illegalAccessError" method="AfterThrowingAdvice" /> -->
        </aop:aspect>
    </aop:config>
    
    <!-- 配置bean -->
    <bean id="Student" class="com.spring.aop1.Student">
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
    </bean>
    <bean id="SeizedAdvice" class="com.spring.aop1.SeizedAdvice"></bean>
</beans>

4.最終的目錄結構,只看標註紅色的地方就可以了

5.運行結果  因爲開了前置通知,和printAdvice方法,所以它先出前置通知然後再顯示調用方法

6.如果是用的maven,添加下面的pom.xml文件即可

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>jar</groupId>
    <artifactId>jar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>Maven項目測試</description>
    <dependencies>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>3.2.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>


    </dependencies>

</project>

結果也可以出來,不過要注意:

    1.jar文件的版本問題,可能會導致衝突報錯,這時還包版本就可以了

    2.當定義通知時避免通知全部加上,如前置通知和異常通知都想顯示,就會報錯。

    3.最容易出錯也就是xml文件中去定義aspect切面,切點。別忘了配置相應的Bean。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章