zoukankan      html  css  js  c++  java
  • Spring 中基于 AOP 的 XML架构的一个实例

    Spring 中基于 AOP 的 XML架构,我们来做第一个AOP实例

    1.第一步:创建一个新项目,首先导入spring 所需的核心jar和AOP的所需要jar。 我这里已经打包好了

       spring 所需的核心jar和AOP的所需要jar: spring 所需的核心jar和AOP的所需要jar

       Spring  AOP实例: Spring  AOP实例

      在项目下创建一个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:2536201485@qq.com
    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。

  • 相关阅读:
    什么是内存碎片
    java堆内存模型
    java内存模型
    java垃圾回收过程
    找出给定的一个字符串中最长的不重复子串,不重复子串即一个子串中不出现两个相同的字符
    学习tomcat(八)
    学习Redis(二)
    学习k8s(四)
    学习docker(三)
    学习MySql(一)
  • 原文地址:https://www.cnblogs.com/ysource/p/12855333.html
Copyright © 2011-2022 走看看