-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Closed
Labels
status: invalidAn issue that we don't feel is validAn issue that we don't feel is valid
Description
Multi-data configuration
error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.hntxrj.tongxin_encrypt.conf.PrimaryConfig required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.type) did not find property 'spring.datasource.type'
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jdbc.XADataSourceWrapper; SearchStrategy: all) did not find any beans of type org.springframework.boot.jdbc.XADataSourceWrapper
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
DBConfig.java
@Configuration
public class DBConfig {
@Bean
@Primary
@ConfigurationProperties("spring.datasource.primary")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("spring.datasource.primary")
public DataSource firstDataSource() {
return firstDataSourceProperties()
.initializeDataSourceBuilder()
.build();
}
@Bean
@ConfigurationProperties("spring.datasource.secondary")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@ConfigurationProperties("spring.datasource.secondary")
public DataSource secondDataSource() {
return secondDataSourceProperties()
.initializeDataSourceBuilder()
.build();
}
}
PrimaryConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryPrimary",
transactionManagerRef="transactionManagerPrimary",
basePackages= { "com.hntxrj.tongxin_encrypt.repository.primary" }) //设置Repository所在位置
public class PrimaryConfig {
private final DataSource primaryDataSource;
private final JpaProperties jpaProperties;
@Autowired
public PrimaryConfig(
@Qualifier("primaryDataSource") DataSource primaryDataSource,
JpaProperties jpaProperties) {
this.primaryDataSource = primaryDataSource;
this.jpaProperties = jpaProperties;
}
@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder)
.getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties())
.packages("com.hntxrj.tongxin_encrypt.entity") //设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit")
.build();
}
private Map<String, Object> getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(
EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(
entityManagerFactoryPrimary(builder).getObject());
}
}
SecondaryConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactorySecondary",
transactionManagerRef="transactionManagerSecondary",
basePackages= { "com.hntxrj.tongxin_encrypt.repository.software" }) //设置Repository所在位置
public class SecondaryConfig {
private final DataSource secondaryDataSource;
private final JpaProperties jpaProperties;
@Autowired
public SecondaryConfig(DataSource secondaryDataSource, JpaProperties jpaProperties) {
this.secondaryDataSource = secondaryDataSource;
this.jpaProperties = jpaProperties;
}
@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(
EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder)
.getObject().createEntityManager();
}
@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties())
.packages("com.hntxrj.tongxin_encrypt.repository.software") //设置实体类所在位置
.persistenceUnit("secondaryPersistenceUnit")
.build();
}
private Map<String, Object> getVendorProperties() {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(
EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(
entityManagerFactorySecondary(builder).getObject());
}
}
application.yml
spring:
datasource:
primary:
url: jdbc:mysql://localhost/tongxin_encrypt?characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
secondary:
url: jdbc:sqlserver://192.168.31.16:1433;DatabaseName=software
username: sa
password: 123456
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
Metadata
Metadata
Assignees
Labels
status: invalidAn issue that we don't feel is validAn issue that we don't feel is valid