Spring入门学习手册 3:AOP是个什么鬼?

609 阅读4分钟

完整代码在这

一、AOP是什么?

AOP的全称是 Aspect Oriented Program(面向切面编程)

按照我的理解,AOP的思想通俗易懂的来说就是将程序的功能分为两类,一类是核心功能,另一类是辅助功能

在非面向切面编程里,核心功能辅助功能总是耦合在一起,比如说进行数据库操作时,日志记录行为的代码总是紧挨着数据库操作(不知道别的大神是什么样,反正我是这样)。这样一来,核心功能(数据库操作)的代码就和辅助功能(记录日志)的代码耦合在一起了。

而在AOP里,核心功能辅助功能的代码是完全分开的,它们可以各自独立修改,而完全不干扰对方,这实际上应该是应用了Java设计模式里的代理模式(改天记得复习一下)。

二、Spring如何应用AOP?

1.准备工作有哪些

  • 要加哪些包?

    spring的核心包就不用说了,除此之外还要加上和AOP有关的包,aspectJ,完整的maven配置文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <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>SpringLearn</groupId>
        <artifactId>SpringDemo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>4.3.11.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>4.3.11.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.3.11.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.11.RELEASE</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.9.1</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.6.8</version>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.8</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
  • 要加哪些类?

    LoggerAspect

    ProductService

    Category

    Product

目录结构如下:

完整代码在这

2.核心功能 和 辅助功能 是怎么分开的?

创建一个核心功能的类 ProductService

package com.learn.service;

import org.springframework.stereotype.Component;

@Component("ps")
public class ProductService {

    public void doSomeService() {
        System.out.println("假装这里是一些核心业务代码!");
    }

}

创建一个辅助功能的类 LoggerAspect

package com.learn.aspect;

import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;

@Component("la")
public class LoggerAspect {
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

可以看到这里核心功能的代码和辅助功能的代码是完全分开的,那么怎么把它们联系到一起呢?

这就需要配置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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
   
    <!--扫描bean-->
    <context:component-scan base-package="com.learn.service com.learn.springBasic com.learn.aspect"/>
    
    <!--配置AOP-->
    <aop:config>
        <!--配置切点-->
        <aop:pointcut id="loggerCutpoint" expression="execution(* com.learn.service.ProductService.*(..)) "/>
        <!--配置织入对象-->
        <aop:aspect id="logAspect" ref="la">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>

</beans>

配置文件中需要加入 <aop:config> 标签。

<aop:pointcut> 标签配置的是核心类,在这里指定了要织入的对象是哪个。

execution(* com.learn.service.ProductService.*(..))

上面这句表达式表示,所有ProductService下的方法都将被织入辅助功能,既在执行该方法时会调用辅助功能。 <aop:aspect> 配置的是辅助类

<aop:around pointcut-ref="loggerCutpoint" method="log"/>

上面标签中的 pointcut-ref 很显然就是要织入的核心类id, method 就是被织入的方法。

运行结果:

start log:doSomeService
假装这里是一些核心业务代码!
end log:doSomeService

运行结果呈现出这样的效果的是因为辅助类中log方法中是这样写的:

System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());

log方法最终返回的是一个Object对象,就本例来说,返回的实际上是ProductService 对象,这里应该是用到了代理模式来实现。

三、用注解方式配置AOP

注解方式配置AOP也很简单,和注解方式配置bean是非常相似的。

首先,spring配置文件中把 <aop:config> 下的标签都删掉,换成 <aop:aspectj-autoproxy/> 标签来启用自动代理。

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

    <!--扫描bean-->
    <context:component-scan base-package="com.learn.service com.learn.springBasic com.learn.aspect"/>
    <!--配置自动代理AOP-->
    <aop:aspectj-autoproxy/>

</beans>

LoggerAspect 类中也要做一些改变,加上@Aspect 和 @Around注解。

package com.learn.aspect;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;

@Aspect
@Component("la")
public class LoggerAspect {

    @Around(value = "execution(* com.learn.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

运行结果:

start log:doSomeService
假装这里是一些核心业务代码!
end log:doSomeService

注解方式AOP完整代码在这