senthil kumar
senthil kumar

Reputation: 237

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

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

@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

@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

i am new to this concept kindly help on this how to implement on above class in my project

Upvotes: 1

Views: 9941

Answers (1)

Didier L
Didier L

Reputation: 20618

I came across this question because I wanted to remove an existing custom DataSource configuration and just rely on DataSourceAutoConfiguration instead.

The thing is that this auto-configuration applies if

  • DataSource (or EmbeddedDatabaseType) is on the classpath; and
  • you don't have a DataSource bean configured; and
  • either
    • you have a spring.datasource.type property configured (for Spring Boot 1.3+), or
    • there is a supported connection pool available (e.g. HikariCP or Tomcat connection pool), or
    • there is an embedded (in-memory) database driver available (such as H2, HSQLDB or Derby) – probably not what you want.

In your case, the second condition fails, since CommonDataSourceConfig declares a 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.

The DataSourceAutoConfiguration should then do its job (based on your properties) and you should be able to inject your DataSource directly with @Autowired.

Upvotes: 3

Related Questions