zoukankan      html  css  js  c++  java
  • SpringBoot中DataSourceAutoConfiguration注解

    最近在学习springBoot时,其中有很大的一个核心的只是点:自动配置。

    通过 DataSourceAutoConfiguration 学习 自动配置是如何实现的。

    自动配置中比较重要的一个点就是 条件化配置?

    1、条件化配置

     JdbcTemplateCondition 的定义

    package com.qxy.readinglist.conditions;
    
    
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    
    
    /**
     * 这个类 只有在 ClassPath里存在J
     */
    public class JdbcTemplateCondition implements Condition {
        @Override
        public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
            try {
                //加载 JdbcTemplate
                conditionContext.getClassLoader().loadClass("org.springframework.jdbc.core.JdbcTemplate");
                return true;
            } catch (ClassNotFoundException e) {
                return false;
            }
        }
    }
    

    如何 JdbcTemplateCondition

    @Bean
    @Conditional(JdbcTemplateCondition.class)//只有满足 JdbcTemplateCondition 条件,才会生成 bean
    public MyService myService(){
        return new MyService();
    }
    

     到此位置,你应该知道 条件化配置是怎么回事了吧,然后继续来看看 DataSourceAutoConfiguration

    @Configuration
    @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
    @EnableConfigurationProperties(DataSourceProperties.class)
    @Import({ DataSourcePoolMetadataProvidersConfiguration.class,
    		DataSourceInitializationConfiguration.class })
    public class DataSourceAutoConfiguration {
       .....      
    }
    

      从上边可以看到,ConditionalOnClass,这个就是一个条件化注释,含义是:ClassPath里边有存在,DataSource.class, EmbeddedDatabaseType.class

    Read the fucking manual and source code
  • 相关阅读:
    解决Xcode 证书过期问题
    Parallel Desktop 问题汇总
    CMS系统学习笔记
    git pull报错 error: Your local changes to the following files would be overwritten by merge
    Mac安装Homebrew
    使用nvm-windows安装NodeJs遇到的问题: Could not retrieve https://nodejs.org/dist/latest/SHASUMS256.txt.
    HTB-靶机-Luke
    HTB-靶机-Fortune
    HTB-靶机-Ypuffy
    HTB-靶机-Sunday
  • 原文地址:https://www.cnblogs.com/qxynotebook/p/9735174.html
Copyright © 2011-2022 走看看