【发布时间】:2020-07-24 02:15:16
【问题描述】:
我是 springboot 新手,我需要在我的项目中设置多个数据库我正在使用 postgresql 这是我的属性文件。
如果我正在运行我的应用程序,那么无论@primay 注释我只提供我能够访问的数据库,但我需要同时访问这两个数据库。
注意:如果我同时添加了 db @primary 注释,那么会出现 postconstructor 0 错误。我在 if else 块中的查询有时我需要访问 abc db 有时我需要访问 xyz db 但是在 springconfig 中我只保留@primary 我得到的那个 db url
我尝试让@primary 都获得异常,并尝试在我的 springconfig 文件中添加@configurationProperties 获得异常
spring.datasource1.url=jdbc:postgresql://100.17.13.26:123/abc
spring.datasource1.username=ENC(vAIVaqTTZ89eYBWBDbUxgGdhciXm3GuB)
spring.datasource1.password=ENC(abZypzjEuvLfbovYs0oGdeRnUqM8e+k1)
spring.datasource1.driver-class-name=org.postgresql.Driver
spring.datasource2.url=jdbc:postgresql://100.17.13.26:123/xyz
spring.datasource2.username=ENC(vAIVaqTTZ89eYBWBDbUxgGdhciXm3GuB)
spring.datasource2.password=ENC(abZypzjEuvLfbovYs0oGdeRnUqM8e+k1)
spring.datasource2.driver-class-name=org.postgresql.Driver
我添加了@springBootApplication SpringConfig 类
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
public class SpringConfiguration {
@Autowired
private Environment env;
@Bean(name = "abc")
public DataSource firstDataSource() {
System.out.println("from first DB");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource1.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource1.url"));
dataSource.setUsername(env.getProperty("spring.datasource1.username"));
dataSource.setPassword(env.getProperty("spring.datasource1.password"));
System.out.println("from first DB end");
return dataSource;
}
@Primary
@Bean(name = "xyz")
public DataSource secondDataSource() {
System.out.println("from second DB");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource2.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource2.url"));
dataSource.setUsername(env.getProperty("spring.datasource2.username"));
dataSource.setPassword(env.getProperty("spring.datasource2.password"));
System.out.println("from second DB end");
return dataSource;
}
@Bean(name = "abc")
public JdbcTemplate template(@Qualifier("abc") DataSource ds) {
System.out.println("calling first db");
return new JdbcTemplate(ds);
}
@Bean(name = "xyz")
public JdbcTemplate template1(@Qualifier("xyz") DataSource ds) {
System.out.println("calling second db");
return new JdbcTemplate(ds);
}
}
【问题讨论】:
标签: java spring postgresql spring-boot