春季启动应用程序。

发布于 2025-01-29 22:15:48 字数 1341 浏览 3 评论 0 原文

我正在学习有关Springboot的知识,并试图连接到DB2数据库。我的工作正常。

以下是我的工作DB2属性:

spring.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
spring.datasource.username=user1
spring.datasource.password=password1

但是我将它们重命名为“ DB2”,而是“ spring”,就像:

db2.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
db2.datasource.username=user1
db2.datasource.password=password1

我的应用程序仍然运行,当我这样做时,我的控制器不再像重命名之前那样返回结果。

我问这样的原因是,如果我将来添加第二个数据源,如果我这样命名,我可以通过其数据源很容易区分属性。

更新:

感谢@kosta tenasis在下面的答案和本文( https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-sring-spring-boot/ ) 。

对数据源进行配置,就可以修改应用程序

db2.datasource.url=...

spring.datasource.url=...

然后回到我的特定问题,一旦您 更改为使用hikari和hikari没有 url 属性,而是使用 jdbc-url ,因此只需

db2.datasource.jdbc-url=...

在数据源中更改以上更改为: note2:您必须在项目中添加多个数据源时必须创建,您将有注释 @ConfigurationProperties 。此注释需要指向您更新的应用程序。数据源( db2.datasource.url )。

I am learning about springboot and trying to connect to a DB2 database. I got that working just fine.

Below are my working DB2 properties:

spring.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
spring.datasource.username=user1
spring.datasource.password=password1

But I renamed them to start with "db2" instead "spring" like:

db2.datasource.url=jdbc:db2://server:port/database:currentSchema=schema-name;
db2.datasource.username=user1
db2.datasource.password=password1

My app still runs, hHowever, when I do that, my controllers no longer return results as they did before the rename.

The reason I ask this is that if I add 2nd data source in the future, I could distinguish easily properties by their data sources if I name them like this.

UPDATE:

Thanks to @Kosta Tenasis answer below and this article (https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-spring-boot/), I was able to resolve and figure this out.

Then going back to my specific question, once you have the configuration for data source in place, you can then modify application.properties to have:

db2.datasource.url=...

instead of having:

spring.datasource.url=...

NOTE1: if you are using Springboot 2.0, they changed to use Hikari and Hikari does not have url property but instead uses jdbc-url, so just change above to:

db2.datasource.jdbc-url=...

NOTE2: In your datasource that you had to create when adding multiple datasources to your project, you will have annotation @ConfigurationProperties. This annotation needs to point to your updated application.properties for datasource (the db2.datasource.url).

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

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

发布评论

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

评论(1

稳稳的幸福 2025-02-05 22:15:48

默认情况下,Spring寻找 Spring.datasource。

因此,您可能会获得错误的结果,因为您没有连接到数据库。如果要配置不同的数据源,则可以从默认值中,您可以这样做的属性

@Configuration
public class DataSourceConfig {

@Bean
@ConfigurationProperties(prefix="db2.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create()
                            .build();
}

,假设一天来了,您想要第二个 datasource ,您可以将上一类修改为类似的类别

@Configuration
public class DataSourceConfig {

@Bean
@ConfigurationProperties(prefix="db2.datasource")
public DataSource d2Datasource() {
    return DataSourceBuilder.create()
                            .build();
}

@Bean
@ConfigurationProperties(prefix="db3.datasource")
public DataSource db3Datasource() { //pun intented
    return DataSourceBuilder.create()
                            .build();
    }
}

:之后,在每个 class 中,您想要 dataSource 您可以指定您喜欢的bean:

public class DB3DependedClass{
    
    private final DataSource dataSource;

    public DB3DependedClass(@Qualifier("db3Datasource") DataSource dataSource){
           this.dataSource = dataSource;
 
    }
}

因此,默认情况下,Spring会寻找

  1. spring.datasource.url(或spring.datasource.jdbc-url)
  2. spring.datasource.username
  3. spring.datasource.passource.password

如果您指定另一个 dataSource of DataSource 您自己的,不需要这些值。

因此,在上面的示例中,我们指定的是 db3.datasource spring将寻找

  1. db3.datasource.url
  2. db3.datasource.username
  3. db3.datasource.password

重要的是, spring 不是推断 完整的路径确实是:<代码> db3.datasource.url
不是
spring.db3.datasource.url

最终要包裹起来,您确实可以灵活地以 spring 开始,如果需要。只要您指定,春季就会接收任一路。

注意:此答案仅写在此处提供的文本框中,并且未在IDE中测试编译错误。逻辑仍然存在

By default Spring looks for spring.datasource.** for the properties of the DataSource to connect to.

So you might be getting wrong results because you are not connecting to the database. If you want to configure a DataSource with different,from default, properties you can do like so

@Configuration
public class DataSourceConfig {

@Bean
@ConfigurationProperties(prefix="db2.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create()
                            .build();
}

And let's say a day comes along and you want a second DataSource you can modify the previous class to something like:

@Configuration
public class DataSourceConfig {

@Bean
@ConfigurationProperties(prefix="db2.datasource")
public DataSource d2Datasource() {
    return DataSourceBuilder.create()
                            .build();
}

@Bean
@ConfigurationProperties(prefix="db3.datasource")
public DataSource db3Datasource() { //pun intented
    return DataSourceBuilder.create()
                            .build();
    }
}

and after that in each Class that you want a DataSource you can specify which of the beans you like:

public class DB3DependedClass{
    
    private final DataSource dataSource;

    public DB3DependedClass(@Qualifier("db3Datasource") DataSource dataSource){
           this.dataSource = dataSource;
 
    }
}

So by default spring will look for

  1. spring.datasource.url (or spring.datasource.jdbc-url)
  2. spring.datasource.username
  3. spring.datasource.password

If you specify another DataSource of your own, those values are not needed.

So in the above example where we specified let's say db3.datasource spring will look for

  1. db3.datasource.url
  2. db3.datasource.username
  3. db3.datasource.password

Important thing here is that the spring IS NOT inferred meaning the complete path is indeed: db3.datasource.url
and NOT
spring.db3.datasource.url

Finally to wrap this up you do have the flexibility to make it start with spring if you want so by declaring a prefix like spring.any.path.ilike.datasouce and of course under that the related values. Spring will pick up either path as long as you specify it.

NOTE: This answer is written solely in the text box provided here and was not tested in an IDE for compilation errors. The logic still holds though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文