我在Spring-boot项目中定义了2个DataSource,我也有2个@Repository。
我的
中有这段代码Application.java
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.opes.kyc_adapter.repository.postgresql", "com.opes.kyc_adapter.repository.mysql"})
@EntityScan(basePackages = {"com.opes.kyc_adapter.model.postgresql", "com.opes.kyc_adapter.model.mysql"})
public class Application implements CommandLineRunner {
@Autowired
private MessageRepositoryMySQL repositoryMySQL;
@Autowired
private MessageRepositoryPostgreSQL repositoryPostgreSQL;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... arg0) throws Exception {
for (int i = 0; i < 5; i++){
repositoryMySQL.save(new MessageOriginal("My post number #" + (i+1)));
repositoryPostgreSQL.save(new MessageExtracted("My post number #" + (i+1), "User #" + (i+1)));
}
}
}
如你所见,我有2个@Repository
每个repo应该将数据添加到不同的DataSource,但两者都添加到MySQL Datasouce。
以下是代码的其余部分:
DataBaseConfigurationMySQL.java
@Configuration
public class DataBaseConfigurationMySQL implements EnvironmentAware {
private RelaxedPropertyResolver jpaPropertyResolver;
@Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
@Override
public void setEnvironment(Environment environment) {
this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, "spring.jpa.");
}
@Bean
@DependsOn("jdbcTemplate")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
if (persistenceUnitManager != null) {
entityManagerFactoryBean
.setPersistenceUnitManager(persistenceUnitManager);
}
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setPackagesToScan("com.opes.kyc_adapter.model.mysql");
entityManagerFactoryBean.getJpaPropertyMap().putAll(jpaPropertyResolver.getSubProperties("properties."));
Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap();
properties.put("hibernate.ejb.naming_strategy", jpaPropertyResolver.getProperty("hibernate.naming-strategy", SpringNamingStrategy.class.getName()));
properties.put("hibernate.hbm2ddl.auto", jpaPropertyResolver.getProperty("hibernate.ddl-auto", "create-drop"));
return entityManagerFactoryBean;
}
@Bean
public DataSource dataSource(){
return DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/kyc-adapter-mysql")
.driverClassName("com.mysql.jdbc.Driver")
.username("root")
.password("pass")
.build();
}
}
DataBaseConigurationPostgreSQL.java
@Configuration
public class DataBaseConfigurationPostgreSQL implements EnvironmentAware {
private RelaxedPropertyResolver jpaPropertyResolver;
@Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
@Override
public void setEnvironment(Environment environment) {
this.jpaPropertyResolver = new RelaxedPropertyResolver(environment, "spring.jpa.");
}
@Bean
@DependsOn("jdbcTemplate")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
if (persistenceUnitManager != null) {
entityManagerFactoryBean
.setPersistenceUnitManager(persistenceUnitManager);
}
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setPackagesToScan("com.opes.kyc_adapter.model.postgresql");
entityManagerFactoryBean.getJpaPropertyMap().putAll(jpaPropertyResolver.getSubProperties("properties."));
Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap();
properties.put("hibernate.ejb.naming_strategy", jpaPropertyResolver.getProperty("hibernate.naming-strategy", SpringNamingStrategy.class.getName()));
properties.put("hibernate.hbm2ddl.auto", jpaPropertyResolver.getProperty("hibernate.ddl-auto", "create-drop"));
return entityManagerFactoryBean;
}
@Bean
public DataSource dataSource(){
return DataSourceBuilder.create()
.url("jdbc:postgresql://localhost:5432/kyc-adapter-postgresql")
.driverClassName("org.postgresql.Driver")
.username("postgres")
.password("pass")
.build();
}
MessageOriginal.java
@Entity
public class MessageOriginal {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String message;
public MessageOriginal(){
}
public MessageOriginal(String message){
this.message = message;
}
//getters and setters
}
MessageExtracted.java
@Entity
public class MessageExtracted {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String message;
private String userName;
public MessageExtracted(){
}
public MessageExtracted(String message, String userName){
this.message = message;
this.userName = userName;
}
//getters and setters
}
MessageRepositoryMySQL.java
@Repository
public interface MessageRepositoryMySQL extends CrudRepository<MessageOriginal, Long>{
}
MessageRepositoryPostgreSQL.java
@Repository
public interface MessageRepositoryPostgreSQL extends CrudRepository<MessageExtracted, Long>{
}