23 Haziran 2020 Salı

SpringBoot DataSourceAutoConfiguration

Örnek - DataSourceAutoConfiguration Yapmamak
Şöyle yaparız
spring.autoconfigure.exclude=
  org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, \
  org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, \
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
Örnek - İki Tane DataSource
application.properties şöyle olsun
# Datasource - Secondary
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true


# Datasource - Primary
spring.hello.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.hello.datasource.url=jdbc:mysql://localhost:3306/batchmetadata
spring.hello.datasource.username=root
spring.hello.datasource.password=root


#spring.batch.table-prefix=batchmetadata.BATCH_
spring.batch.initialize-schema=always
spring.batch.job.enabled=false
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

#https://stackoverflow.com/questions/28275448/multiple-data-source-and-schema-creation-in-spring-boot
Elimizde şöyle bir kod olsun.
@Configuration
public class DatabaseConfig {
  @Autowired
  private Environment env;

  // For Test schema
  @Bean(name="secondaryDS")
  @ConfigurationProperties(prefix="spring.datasource")
  public DataSource getSeconadaryBatchDataSource(){
    return DataSourceBuilder.create()
      .url(env.getProperty("spring.datasource.url"))
      .driverClassName(env.getProperty("spring.datasource.driver-class-name"))
      .username(env.getProperty("spring.datasource.username"))
      .password(env.getProperty("spring.datasource.password"))
      .build();
}


  // For "batchmetadata" tables
  @Bean(name="primaryDS")
  @Primary
  @ConfigurationProperties(prefix="spring.hello.datasource")
  public DataSource getPrimaryBatchDataSource(){
    return DataSourceBuilder.create()
      .url(env.getProperty("spring.hello.datasource.url"))
      .driverClassName(env.getProperty("spring.hello.datasource.driver-class-name"))
      .username(env.getProperty("spring.hello.datasource.username"))
      .password(env.getProperty("spring.hello.datasource.password"))
      .build();
  }
...
}
Şöyle yaparız
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory
  (EntityManagerFactoryBuilder builder) {
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put("hibernate.hbm2ddl.auto", "update");
  return builder
    dataSource(getSeconadaryBatchDataSource())
    .packages("com.example.model")
    .persistenceUnit("default")
    .properties(properties)
    .build();
}

Hiç yorum yok:

Yorum Gönder