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

发布于 2025-02-06 05:00:30 字数 63 浏览 2 评论 0原文

我需要建立与DB2数据库的连接,但是我无法使用Application.properties,因此如何创建此连接?

I need to build a connection to db2 database, but I can not use application.properties at this moment, so how can I create this connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2025-02-13 05:00:30

您可以在应用程序本身中实现dataSource bean。例如:

@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();
  }

}

然后,请确保对数据源的任何自动引用为“懒惰”,因此该应用程序有时间加载属性(也许来自另一个文件)并实例化数据源,如:

  @Lazy
  @Autowired
  private DataSource dataSource;

You can implement a datasource bean in the app itself. 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文