使用 Spring Boot 我可以实例化一个JdbcTemplate
具有以下内容:
Code:
@Autowired
private JdbcTemplate jdbcTemplate;
特性:
spring.datasource.url=jdbc:postgresql://my_url:my_port/my_other_stuff
spring.datasource.username=my_user_name
spring.datasource.password=my_password
spring.datasource.driver-class-name=org.postgresql.Driver
这将创建一个 DataSource 类:org.apache.tomcat.jdbc.pool.DataSource
如何以编程方式设置数据源用户名/密码?
我们有一项政策,不以纯文本形式存储凭据,而我必须在工作地点使用特定的凭据提供商。
您可以使用DataSourceBuilder
如果你正在使用jdbc
起动机。另外,为了覆盖默认的自动配置 bean,您需要将您的 bean 标记为@Primary
就我而言,我的属性以datasource.postgres
prefix.
E.g
@ConfigurationProperties(prefix = "datasource.postgres")
@Bean
@Primary
public DataSource dataSource() {
return DataSourceBuilder
.create()
.build();
}
如果它对您来说不可行,那么您可以使用
@Bean
@Primary
public DataSource dataSource() {
return DataSourceBuilder
.create()
.username("")
.password("")
.url("")
.driverClassName("")
.build();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)