当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot 源码解析 (十)----- Spring Boot 精髓:集成AOP

SpringBoot 源码解析 (十)----- Spring Boot 精髓:集成AOP

2019年11月27日  | 移动技术网IT编程  | 我要评论

本篇主要集成sping一个重要功能aop

我们还是先回顾一下以前spring中是如何使用aop的,大家可以看看我这篇文章spring5 源码深度解析----- aop的使用及aop自定义标签

spring中使用aop

引入aspect

<dependency>
    <groupid>org.aspectj</groupid>
    <artifactid>aspectjweaver</artifactid>
    <version>${aspectj.version}</version>
</dependency>

创建用于拦截的bean

public class testbean {
    private string message = "test bean";

    public string getmessage() {
        return message;
    }

    public void setmessage(string message) {
        this.message = message;
    }

    public void test(){
        system.out.println(this.message);
    }
}

创建advisor

@aspect
public class aspectjtest {
    @pointcut("execution(* *.test(..))")
    public void test(){
    }
    
    @before("test()")
    public void beforetest(){
        system.out.println("beforetest");
    }
    
    @around("test()")
    public object aroundtest(proceedingjoinpoint p){
        system.out.println("around.....before");
        object o = null;
        try{
            o = p.proceed();
        }catch(throwable e){
            e.printstacktrace();
        }
        system.out.println("around.....after");
        return o;
    }
    
    @after("test()")
    public void aftertest()
    {
        system.out.println("aftertest");
    }
 }

创建配置文件

要在spring中开启aop功能,还需要在配置文件中作如下声明,开启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: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">

    <aop:aspectj-autoproxy/>
    <bean id="test" class="com.yhl.myspring.demo.aop.testbean">
        <property name="message" value="这是一个苦逼的程序员"/>
    </bean>
    <bean id="aspect" class="com.yhl.myspring.demo.aop.aspectjtest"/>
</beans>

注解开启aop

开启aop<aop:aspectj-autoproxy/>也可以使用注解的方式,如下,使用@enableaspectjautoproxy配置在任何一个@configratrion或者@component上

springboot集成aop

添加pom依赖

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-aop</artifactid>
</dependency>

引入了aop的场景启动器,我们点击去看看

还是引入了spring-aop和aspectj的依赖,和我们spring集成aop是引入了相同的包,接着我们直接就可以创建advisor了,如上aspectjtest这个类,但是我们并没有通过@enableaspectjautoproxy开启aop呢?那是因为aop的自动配置类帮我们开启了

aopautoconfiguration

一旦导入了spring-boot-starter-aop依赖后,springboot就会启动aop的自动配置类aopautoconfiguration:

我们来看看aopautoconfiguration这个自动配置类

@configuration
@conditionalonclass({ enableaspectjautoproxy.class, aspect.class, advice.class })
@conditionalonproperty(prefix = "spring.aop", name = "auto", havingvalue = "true", matchifmissing = true)
public class aopautoconfiguration {

    @configuration
    //使用注解开启aop功能
    @enableaspectjautoproxy(proxytargetclass = false)
    @conditionalonproperty(prefix = "spring.aop", name = "proxy-target-class", havingvalue = "false", matchifmissing = true)
    public static class jdkdynamicautoproxyconfiguration {

    }

    @configuration
    //使用注解开启aop功能
    @enableaspectjautoproxy(proxytargetclass = true)
    @conditionalonproperty(prefix = "spring.aop", name = "proxy-target-class", havingvalue = "true", matchifmissing = false)
    public static class cglibautoproxyconfiguration {

    }

}

不管使用jdk代理还是cglib代理,都有@enableaspectjautoproxy注解,所以只要导入spring-boot-starter-aop依赖后,就自动帮我们开启了aop,我们可以直接添加切面使用aop了

@enableaspectjautoproxy这个注解是整个aop的灵魂,其作用和<aop:aspectj-autoproxy/>是一样的,大家可以看看其源码分析spring5 源码深度解析----- aop的使用及aop自定义标签

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网