我正尝试在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
secondary.datasource.jdbc-url=jdbc:as400://xxxxxx;
secondary.datasource.driverClassName=com.ibm.as400.access.AS400JDBCDriver
secondary.datasource.username=xxxxx
secondary.datasource.password=xxxxxx
secondary.datasource.testOnBorrow=true
secondary.datasource.validation-query=select 1 from sysibm.sysdummy1
我可以知道我做错了什么吗,请指教。提前感谢
发布于 2020-10-04 10:23:42
我能想到的原因之一是,您添加了这个前缀
@ConfigurationProperties(prefix = "secondary.datasource")
然后使用完整路径访问driverClassName
ds.setDriverClassName(env.getProperty("secondary.datasource.driverClassName"));
我认为你应该去掉前缀并尝试访问。这个博客也可能对你有所帮助。https://howtodoinjava.com/spring-boot2/datasource-configuration/
https://stackoverflow.com/questions/64190590
复制相似问题