我正尝试在spring boot 2应用程序中建立2个数据库连接。我得到了下面的错误:
java.lang.IllegalArgumentException: Property 'driverClassName' must not be empty
这是我在主数据库上的配置
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "myEntityManagerFactory", basePackages = {"com.cmp.myapp.repository"}) public class PrimDBConfig { @Primary @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); @PersistenceContext(unitName = "primdb") @Primary @Bean(name = "entityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) { return builder .dataSource(dataSource) .packages("com.cmp.myapp.dto") .persistenceUnit("primdb") .build(); @Primary @Bean(name = "transactionManager") public PlatformTransactionManager transactionManager( @Qualifier("myEntityManagerFactory") EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); }
下面是辅助数据库连接
@Configuration public class SecDBConfig { @Autowired Environment env; @Bean(name = "secDataSource") @ConfigurationProperties(prefix = "secondary.datasource") public DataSource wiseDataSource() { DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setUrl(env.getProperty("secondary.datasource.jdbc-url")); ds.setDriverClassName(env.getProperty("secondary.datasource.driverClassName")); ds.setUsername(env.getProperty("secondary.datasource.username")); ds.setPassword(env.getProperty("secondary.datasource.password")); return ds; }
我的application.properties文件如下:
spring.datasource.jdbc-url=jdbc:sqlserver://xxx spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.username=xxxx spring.datasource.password=xxxx spring.datasource.validation-query=select 1 spring.datasource.testOnBorrow=true