[英]how to implement DataSourceAutoConfiguration in spring boot
I have 5 micro services with different database name so apart from every properties is common so included in application.properties
我有 5 个具有不同数据库名称的微服务,因此除了每个属性都是通用的,因此包含在
application.properties
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.username=${local.db.username:}
spring.datasource.password=${local.db.password:}
And i had class commondatasource.java which included properties我有类 commondatasource.java 包含属性
@PropertySource({ "classpath:application-test.properties" })
@Component
public class CommonDataSourceConfig {
@Autowired
private Environment env;
@Primary
@Bean
public DataSource dataadmindataSource()
{
final DataSource dataSource = new DataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("spring.datasource.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull("spring.datasource.url"));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("spring.datasource.username")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("spring.datasource.password")));
}
}
now i want to call this commondatasource in every micro services datasourceconfig.java现在我想在每个微服务 datasourceconfig.java 中调用这个commondatasource
@Configuration
@EnableJpaRepositories(basePackages = {
"xxx.repositories" }, entityManagerFactoryRef = "xxEntityManager",
transactionManagerRef = "xxTransactionManager")
public class xxSourceConfig
{
@Autowired
private Environment env;
@Autowired
private CommonDataSourceConfig common;
@Value("${xx.datasource.url}")
private String url;
/**
* Configures the entity manager
*
* @return
*/
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean dataAdminEntityManager()
{
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(common.dataadmindataSource());
entityManager.setPackagesToScan(new String[] { "com.boeing.toolbox.dataadmin.domain" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManager.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
entityManager.setJpaPropertyMap(properties);
return entityManager;
}
}
but now i want to implement by this class https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java但现在我想通过这个类实现https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc /DataSourceAutoConfiguration.java
i am new to this concept kindly help on this how to implement on above class in my project我是这个概念的新手,请帮助解决如何在我的项目中的上述课程中实施
I came across this question because I wanted to remove an existing custom DataSource
configuration and just rely on DataSourceAutoConfiguration
instead.我遇到这个问题是因为我想删除现有的自定义
DataSource
配置,而只依赖DataSourceAutoConfiguration
。
The thing is that this auto-configuration applies if问题是这种自动配置适用于
DataSource
(or EmbeddedDatabaseType
) is on the classpath; DataSource
(或EmbeddedDatabaseType
)在类路径上; andDataSource
bean configured;DataSource
bean; andspring.datasource.type
property configured (for Spring Boot 1.3+), orspring.datasource.type
属性(适用于 Spring Boot 1.3+),或 In your case, the second condition fails, since CommonDataSourceConfig
declares a DataSource
bean.在您的情况下,第二个条件失败,因为
CommonDataSourceConfig
声明了一个DataSource
bean。 The auto-configuration thus backs-off.自动配置因此退避。
You should thus remove that bean, and make sure that the 3rd condition is also satisfied by either setting the spring.datasource.type
or, probably better, putting a compatible connection pool on the classpath.因此,您应该删除该 bean,并确保通过设置
spring.datasource.type
或在类路径上放置兼容的连接池,也满足第三个条件。
The DataSourceAutoConfiguration
should then do its job (based on your properties) and you should be able to inject your DataSource
directly with @Autowired
.然后
DataSourceAutoConfiguration
应该完成它的工作(基于您的属性)并且您应该能够直接使用@Autowired
注入您的DataSource
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.