zoukankan      html  css  js  c++  java
  • Spring-AOP SpringBoot自动配置和启动Spring AOP

    SpringBoot 会使用 @Conditional* 注解来进行判断是否需要自动启动 AOP,如果 classpath 下有 spring-aop 的 jar 和有 EnableAspectJAutoProxy 类等,它就会自动开启 spring-aop。并且此自动配置类还能通过 SpringBoot 的配置文件 application.properties 中配置的 AOP 相关属性进行选择,使用哪一种代理模式,非常智能。

    package org.springframework.boot.autoconfigure.aop;
    
    @Configuration
    @ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.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 = true)
    	public static class JdkDynamicAutoProxyConfiguration {
    
    	}
    
    	@Configuration
    	@EnableAspectJAutoProxy(proxyTargetClass = true)
    	@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = false)
    	public static class CglibAutoProxyConfiguration {
    
    	}
    
    }
    

    总结

    如果我们使用的 SpringBoot,只需要在 properties 文件中进行相关配置,或者不配置直接采用默认配置即可,我们不需要在启动类加上启动 Aop 的注解 EnableAspectJAutoProxy

    如果我们使用的是非Springboot模式,我们需要在配置类上加上 EnableAspectJAutoProxy 此注解。

  • 相关阅读:
    毕业两年
    Python & PyCharm & Django 搭建web开发环境(续)
    Python & PyCharm & Django 搭建web开发环境
    Jboss7 部署EJB3 简明教程
    java 、HashMap 和单例
    一个Flex 对话框的坑
    一道文本处理题目的思考
    synchronized 与 Lock 的那点事
    推荐5款简洁美观的Hexo主题
    【HTTP缓存】浏览器缓存理论知识
  • 原文地址:https://www.cnblogs.com/leihuazhe/p/9697431.html
Copyright © 2011-2022 走看看