[英]How can I create a db2 connection in Spring boot without use application.properties?
我需要建立一个到 db2 数据库的连接,但是我现在不能使用 application.properties,那么我该如何创建这个连接呢?
You can implement a datasource
bean in the app itself.您可以在应用程序本身中实现datasource
bean。 For example:例如:
@Configuration
public class MyClass {
@Bean
public DataSource getDataSource() {
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName(JDBC_DRIVER_CLASS_NAME);
dataSourceBuilder.url(myprops.getJDBCUrl());
dataSourceBuilder.username(myprops.getJDBCUsername());
dataSourceBuilder.password(myprops.getJDBCPassword());
return dataSourceBuilder.build();
}
}
Then, make sure any autowired references to the data source are "lazy", so the app has time to load the properties (maybe from another file) and to instantiate the data source, as in:然后,确保对数据源的任何自动装配引用都是“惰性”的,因此应用程序有时间加载属性(可能来自另一个文件)并实例化数据源,如下所示:
@Lazy
@Autowired
private DataSource dataSource;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.