当前位置: 首页 > >

Spring Boot ¼òµ¥ÅäÖÃ×Ô¶¨ÒåÊý¾ÝÔ´ DataSource

发布时间:

Spring Boot ÒѾ­ÊµÏÖÁË×Ô¶¯¼ÓÔØ DataSource µÄÏà¹ØÅäÖá£ÎÒÃÇÖ»ÐèÒªÔÚÏîÄ¿ÖÐÒýÓÃÏà¹ØÒÀÀµ£¬ÔÚÅäÖÃÎļþ application.yaml? ÖÐÅäÖÃÏà¹ØÊôÐÔ¼´¿É¡£Ïà¹ØÏà¹ØÔ´ÂëÈë¿ÚΪ? org.springframework.boot.autoconfigure.jdbc °üÏ嵀 DataSourceAutoConfiguration ÅäÖÃÀà¡£


?


£¨Ò»£© Spring Boot Æô¶¯»áµ÷ÓÃ?DataSourceAutoConfiguration ¡£²¿·ÖÔ´ÂëÈçÏ£º


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

private static final Log logger = LogFactory
.getLog(DataSourceAutoConfiguration.class);

@Bean
@ConditionalOnMissingBean
public DataSourceInitializer dataSourceInitializer(DataSourceProperties properties,
ApplicationContext applicationContext) {
return new DataSourceInitializer(properties, applicationContext);
}

/**
* Determines if the {@code dataSource} being used by Spring was created from
* {@link EmbeddedDataSourceConfiguration}.
* @param beanFactory the bean factory
* @return true if the data source was auto-configured.
*/
public static boolean containsAutoConfiguredDataSource(
ConfigurableListableBeanFactory beanFactory) {
try {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition("dataSource");
return EmbeddedDataSourceConfiguration.class.getName()
.equals(beanDefinition.getFactoryBeanName());
}
catch (NoSuchBeanDefinitionException ex) {
return false;
}
}
????
????.......

}

µ«ÊÇÓÐЩ¹«Ë¾ÒªÇó£¬Êý¾Ý¿âµÄÏà¹ØÅäÖã¬ÓÈÆäÊÇÃÜÂ룬²»ÄÜÃ÷ÎÄ·ÅÔÚÅäÖÃÎļþÖУ¬ÐèÒªµ÷Óù«Ë¾Ïà¹*½Ì¨ÌṩµÄ½Ó¿ÚÀ´»ñÈ¡Êý¾Ý¿â¼ÓÃܺóµÄÃÜÂë½øÐÐÅäÖ㬱¾Îļ´Êǽ²½âÈçºÎͨ¹ý×Ô¶¨Òå DataSourceAutoConfiguration?ÅäÖÃÊý¾ÝÔ´ DataSource


£¨¶þ£© ÏîÄ¿Öг£Ê¹ÓÃµÄ DataSource Ϊ°¢ÀïµÄ druid ¡£Ïà¹Ø²Î¿¼?https://github.com/alibaba/druid


ÏîÄ¿ÖÐн¨ÅäÖÃÀà DataSourceProperties£¬´úÂëÈçÏ£º


@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {

@Resource DataSourceProperties properties;

@Bean
public DataSource dataSource() {
????????//¿ÉÒÔÔÚ´Ë´¦µ÷ÓÃÏà¹Ø½Ó¿Ú»ñÈ¡Êý¾Ý¿âµÄÅäÖÃÐÅÏ¢½øÐÐ DataSource µÄÅäÖÃ
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setDriverClassName(properties.getDriverClassName());
return dataSource;
}


}

дºÃÅäÖÃÀàºó£¬»¹ÓÐÒ»²½ºÜ¹Ø¼ü


£¨Èý£© ´´½¨ spring.factories


ÔÚ resources ÏÂÃæ´´½¨ META-INF/spring.factories , È»ºóÔÚÎļþÖÐ°ÑµÚ £¨¶þ£© ²½µÄÀàÅäÖýøÈ¥£¬ÄÚÈÝÈçÏ£º


org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sinosoft.configurer.database.DataSourceAutoConfiguration

?


相关推荐


友情链接: