Spring学习之旅(五)极速创建Spring AOP java工程项目

编译工具:eclipse。

简单说一下,Spring  AOP是干嘛的?

假设你创建了一群类:类A,类B,类C,类D。。。。

现在你想为每个类都增加一个新功能,那么该怎么办呢?是不是想到了为每个类增加相同的新代码。这未免也Boring了吧。不过不用担心,Spring AOP就是来帮组你脱离这种单调乏味无聊愚蠢的工作的。

例子:创建类A,执行类A的方法。添加类A方法执行前后的时间显示。

1)新建Java工程项目

假设工程名为:spring_aop_hello

2)导入AOP所需jar包

注意,其中aspectjrt.jar和aspectjweaver.jar并不包含在Spring框架相关包里面。

需另外下载(附下载地址:http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.8.13.jar

另,下载后需解压,相关详细操作请看博客(http://blog.csdn.net/u012453843/article/details/52347208

3)在src目录下创建Spring配置文件,并在配置文件中引入AOP命名空间(相关版本的命名空间可在官网查询)

本例相关代码稍后在本篇贴出。

4)编写业务逻辑所涉及的有关类完成原始业务需求。

创建类A:

package com.edu.aop.hello;

public class A {

    private String a1;
    private String a2;
    public void methodA1(){
        System.out.println("A类的方法1被执行");
    }
    public void methodA2(){
        System.out.println("A类的方法2被执行");
    }
    public A(){}
    public A(String a1,String a2){
        this.a1=a1;
        this.a2=a2;
    }
    
//省略相关setter/getter方法
    
}

配置文件aop_hello.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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 配置bean -->
<bean id="a" class="com.edu.aop.hello.A"></bean>

</beans>

测试主方法:

package com.edu.aop.hello;

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

public class Main {

    public static void main(String[] args) {
        //创建IoC容器
        @SuppressWarnings("resource")
        ApplicationContext ctx=new ClassPathXmlApplicationContext("aop_hello.xml");
        //从容器中取出对象
        A a=(A)ctx.getBean("a");
        //引用对象,按业务逻辑要求依次执行有关的方法
        a.methodA1();
        a.methodA2();

    }

}

运行结果:

以上为原始业务逻辑,下面涉及AOP。

5)采用“AOP编程”完成新添业务需求

创建切片类X:

package com.edu.aop.hello;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.aspectj.lang.JoinPoint;


public class X {

    //连接点对象做参考,利用该参数,获取目标对象的方法
    public void methodX1(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();//获取方法名
        System.out.println("我是前置通知,在A类方法:"+methodName+"()运行前被切入");
        //获取当前系统时间,并转换为yyyy年MM月dd日     HH:mm:ss格式,并显示
        Calendar startTimeNow=Calendar.getInstance();
        SimpleDateFormat fmt=new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss");
        String startTime=fmt.format(startTimeNow.getTime());
        System.out.println("A类方法:"+methodName+"(),运行时间:"+startTime);
    }
    
    
    //连接点对象做参数,利用该参数,获取目标对象的方法
    public void methodX2(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();//获取方法名
        System.out.println("我是后置通知,在A类方法:"+methodName+"()运行后切入");
        //获取当前系统时间,并转换为yyyy年MM月dd日     HH:mm:ss格式,并显示
        Calendar endTimeNow=Calendar.getInstance();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss");
        String endTime=sdf.format(endTimeNow.getTime());
        System.out.println("A类方法:"+methodName+"()运行结束时间:"+endTime);
        
    }
}

在配置文件内配置切片,配置完成后的文件代码为:

<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置bean -->
<bean id="a" class="com.edu.aop.hello.A"></bean>

<!-- 将切面类声明配置成一个bean -->
<bean id="x" class="com.edu.aop.hello.X"></bean>

<aop:config>
<aop:aspect ref="x">
<!-- 配置前置通知及前置通知的切入点 -->
<aop:before method="methodX1" pointcut="execution(* com.edu.aop.hello.A.*())"></aop:before>
<!-- 配置后置通知及后置通知的切入点 -->
<aop:after method="methodX2" pointcut="execution(* com.edu.aop.hello.A.*())"></aop:after>
</aop:aspect>
</aop:config>
</beans>

6)部署运行。

运行结果为:

参考书籍《Java EE框架开发技术与案例教程》

原文地址:https://www.cnblogs.com/dudududu/p/8482487.html