一、什么是AOP

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

二、AOP的一些概念

1.Aspect(切面):切面是通知和切入点的结合。

2.Join point(连接点):与切入点匹配的执行点,例如执行方法或处理异常。在SpringAOP 中,连接点始终表示方法执行。

3.Advice(通知):在切面中需要完成的工作。

4.Pointcut(切入点):切入通知执行的位置

5.Target(目标):被通知的对象。

6.proxy(代理):向目标对象应用通知之后创建的对象。

7.Introduction(引入):向现有类添加新的方法或属性。

8.Weaving(织入):将各个方面与其他应用程序类型或对象链接起来,以创建通知的对象。

三、AOP的三种实现方法

首先我们需要导入依赖:

     <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>

1.通过SpringAPI接口进行实现

SpringAOP有五种通知方式,也有对应的接口:

Before:前置通知,在目标方法调用前通知,对应接口:MethodBeforeAdvice;

After:后置通知,在目标方法返回或异常后通知,对应接口:AfterReturningAdvice;

AfterReturning:后置返回通知,在目标方法返回后通知,对应接口:AfterReturningAdvice;

AfterThrowing:异常通知,在目标方法抛出异常后通知,对应接口:ThrowsAdvice;

Around:环绕通知:通知方法会将目标方法包裹起来,对应接口:MethodInterceptor;

我们下面以具体的例子来展示:

(1)定义一个接口

package com.jms.service;
public interface UserService {
    void create();
    void read();
    void update();
    void delete();
}

(2)接口的实现类

package com.jms.service;
public class UserServiceImpl implements UserService{
    @Override
    public void create() {
        System.out.println("建立了一个用户信息");
    }
    @Override
    public void read() {
        System.out.println("读取了一个用户信息");
    }
    @Override
    public void update() {
        System.out.println("更新了一个用户信息");
    }
    @Override
    public void delete() {
        System.out.println("删除了了一个用户信息");
    }
}

(3)建立两个类分别继承前置通知和后置返回通知的接口

package com.jms.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class beforeLog implements MethodBeforeAdvice {
    @Override
    //method:要执行目标对象的方法
    //args:参数
    //target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("[Debug]" + target.getClass().getName() + "的" + method.getName() + "执行...");
    }
}
package com.jms.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class afterLog implements AfterReturningAdvice{
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("[Debug]" + target.getClass().getName() + "的" + method.getName() + "执行完成,返回了" + returnValue);
    }
}

(4)xml配置文件

注册bean,并且配置aop

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.jms.service.UserServiceImpl"/>
    <bean id="beforeLog" class="com.jms.log.beforeLog"/>
    <bean id="afterLog" class="com.jms.log.afterLog"/>
    <!--方法一:使用SpringAPI接口-->
    <!--aop配置-->
    <aop:config>
        <!--pointcut:切入点
            expression:表达式
        -->
        <aop:pointcut id="pointcut" expression="execution(* com.jms.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

(5)测试

@Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        /*
        UserServiceImpl userService = applicationContext.getBean("userService", UserServiceImpl.class)这样执行会报错
        因为动态代理代理的是接口,所以必须获取接口
         */
        UserService userService = applicationContext.getBean("userService", UserService.class);
        userService.create();
    }

测试结果如下:

Spring(十四):SpringAOP及AOP的三种实现方法

2.自定义类、自定义切面实现

接口以及实现类都与上面相同

(1)自定义切面类

package com.jms.diy;
public class diyAspect {
    public void before() {
        System.out.println("[Debug]方法执行...");
    }
    public void after() {
        System.out.println("[Debug]方法执行完成");
    }
}

切面类中自定义通知方法

(2)xml配置文件

注入切面类的Bean,配置AOP

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="userService" class="com.jms.service.UserServiceImpl"/>
    <!--方法二:自定义类,自定义切面-->
    <bean id="diyAspect" class="com.jms.diy.diyAspect"/>
    <aop:config>
        <!--自定义切面-->
        <aop:aspect ref="diyAspect">
            <!--切入点-->
            <aop:pointcut id="pointcut1" expression="execution(* com.jms.service.UserServiceImpl.*(..))"/>
            <aop:after method="after" pointcut-ref="pointcut1"/>
            <aop:before method="before" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
</beans>

(3)测试如下

Spring(十四):SpringAOP及AOP的三种实现方法

3.通过注解实现

这种实现其实是第二种的注解方式

(1)自定义切面类

package com.jms.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class annotationAspect {
    @Before("execution(* com.jms.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("[Debug]方法前置增强");
    }
    @After("execution(* com.jms.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("[Debug]方法后置增强");
    }
}

(2)此处还是采用xml配置,也可以采用java类配置

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="userService" class="com.jms.service.UserServiceImpl"/>
    <!--方法三:注解-->
    <!--增加注解支持-->
    <context:annotation-config/>
    <context:component-scan base-package="com.jms"/>
    <aop:aspectj-autoproxy/>
</beans>

(3)测试

Spring(十四):SpringAOP及AOP的三种实现方法

(本文仅作个人学习记录用,如有纰漏敬请指正)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。