简体   繁体   English

如何在 Spring Boot 中实现 DataSourceAutoConfiguration

[英]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 )在类路径上; and
  • you don't have a DataSource bean configured;您没有配置DataSource bean; and
  • either任何一个
    • you have a spring.datasource.type property configured (for Spring Boot 1.3+), or您配置了spring.datasource.type属性(适用于 Spring Boot 1.3+),或
    • there is a supported connection pool available (eg HikariCP or Tomcat connection pool), or有可用的受支持连接池(例如 HikariCP 或 Tomcat 连接池),或
    • there is an embedded (in-memory) database driver available (such as H2, HSQLDB or Derby) – probably not what you want.有可用的嵌入式(内存中)数据库驱动程序(例如 H2、HSQLDB 或 Derby)——可能不是您想要的。

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.

相关问题 如何扩展Spring Boot的DataSourceAutoConfiguration - How to extend Spring Boot's DataSourceAutoConfiguration 禁用 DataSourceAutoConfiguration Spring 启动 - Disable DataSourceAutoConfiguration Spring boot Spring Boot-创建名称为&#39;org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration&#39;的bean时出错 - Spring Boot - Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration' 如何实现 Spring Boot + Hibernate - How to implement Spring boot + Hibernate 从自动配置中排除DataSourceAutoConfiguration.class时如何注入spring.datasource。*属性 - How to inject spring.datasource.* properties when DataSourceAutoConfiguration.class is excluded from autoconfiguration 如何在 Spring Boot 中实现装饰模式 - How to implement Decorator pattern in Spring Boot 如何在 Spring Boot + Hibernate 中实现“删除”方法 - How to implement "delete" method in Spring Boot + Hibernate 如何在Spring Boot中使用DynamoDB实施审核? - How to implement Auditing with DynamoDB in Spring Boot? 如何使用Spring Boot实施“负载均衡器”? - How to implement “load balancer” using spring boot? 如何在Spring Boot中实现基本身份验证? - How to implement basic authentication in Spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM