简体   繁体   English

如何在不使用 application.properties 的情况下在 Spring Boot 中创建 db2 连接?

[英]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.

相关问题 如何以编程方式覆盖 Spring Boot application.properties? - How can I override Spring Boot application.properties programmatically? 如何在 spring 引导中创建每个模块 application.properties? - How to create per module application.properties in spring boot? 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? Spring Boot application.properties - Spring Boot application.properties 我应该如何为Spring创建一个Java application.properties文件? - How should I create a Java application.properties file for Spring? 具有@ConfigurationProperties的Spring Boot应用程序,不带标准application.properties - Spring Boot application with @ConfigurationProperties without standard application.properties 如何从Spring Boot Config Server服务的其他属性文件中的application.properties获取密钥? - How can I get the keys from application.properties within other properties files served by Spring Boot Config Server? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何在Spring安全上下文配置xml文件中使用application.properties中的布尔参数? - How can I use boolean parameters from application.properties in spring security context configuration xml file? Spring 引导读取 application.properties /.yml 变量,不带注释 - Spring Boot read application.properties / .yml variable without annotations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM