美文网首页
带着问题看源码-spring aop(一)

带着问题看源码-spring aop(一)

作者: java_舸 | 来源:发表于2019-06-17 19:54 被阅读0次

问题一:Spring Boot的默认配置在哪

  • Spring使用aop的时候会在配置类上加上@EnableAspectJAutoProxy开启aop功能,Spring Boot在引入spring-boot-starter-aop之后并不需要做其他配置,aop功能是怎么开启的?

pom文件中添加spring-boot-starter-aop依赖之后,依赖包aspectjweaver-1.9.4.jar[目前使用到的boot版本是2.1.5]添加进来,在spring-boot-autoconfigure自动配置模块中可以看到aop自动配置类的源码

@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
      AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true",
      matchIfMissing = true)
public class AopAutoConfiguration {

  @Configuration
  @EnableAspectJAutoProxy(proxyTargetClass = false)
  @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class",
          havingValue = "false", matchIfMissing = false)
  public static class JdkDynamicAutoProxyConfiguration {

  }

  @Configuration
  @EnableAspectJAutoProxy(proxyTargetClass = true)
  @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class",
          havingValue = "true", matchIfMissing = true)
  public static class CglibAutoProxyConfiguration {

  }

}

因为aspectjweaver-1.9.4.jar添加之后,@ConditionalOnClass这个注解需要的条件已经可以满足了.spring.aop.auto这个属性我们并没有在自己的配置文件中配置,我们可以在spring-boot-autoconfigure这个模块中的META-INFO/spring-configuration-metadata.json中找到如下配置信息

{
     "name": "spring.aop.auto",
     "type": "java.lang.Boolean",
     "description": "Add @EnableAspectJAutoProxy.",
     "defaultValue": true
},
{
     "name": "spring.aop.proxy-target-class",
     "type": "java.lang.Boolean",
     "description": "Whether subclass-based (CGLIB) proxies are to be created >>(true), as opposed to standard Java interface-based proxies (false).",
     "defaultValue": true
}

可以看出来spring.aop.auto是spring boot的默认配置属性,所以@ConditionalOnProperty所需要的条件也满足了,AopAutoConfiguration这个组件会在spring容器中创建.因为spring.aop.proxy-target-class这个属性的默认配置为true,所以CgligAutoProxyConfiguration这个组件会在spring容器中创建.可以看出来spring boot2.1.5这个版本aop的代理默认使用的cglib动态代理.

  • spring boot默认的配置信息可以在哪里查看
  • 方式一:可以在spring-boot-autoconfigure这个模块中的META-INFO/spring-configuration-metadata.json中查看
  • 方式二:可以通过spring boot的官网提供的docs进行查看

相关文章

网友评论

      本文标题:带着问题看源码-spring aop(一)

      本文链接:https://www.haomeiwen.com/subject/zllsfctx.html