How to configure JNDI Datasource with embedded Tomcat for Spring Data JDBC using Spring Boot 3.1.1 that will also deploy to external Tomcat?

huangapple go评论63阅读模式
英文:

How to configure JNDI Datasource with embedded Tomcat for Spring Data JDBC using Spring Boot 3.1.1 that will also deploy to external Tomcat?

问题

我很好奇是否有人有一个模式来使用最新版本的Spring Boot解决这个问题?我的IT部门托管他们自己的Tomcat服务器,在我切换到Spring Boot之前,我们会创建自己的DataSource配置类,以从服务器的Context.xml中获取正确的数据库连接信息,因为IT管理他们自己的数据库凭据,并只给我们JNDI路径。然后,在本地开发时,将使用本地的Context.XML来执行相同的操作,使用相同的JNDI路径,但使用我颁发的凭据。我可以在嵌入式Tomcat中执行这个操作,以便在部署到由IT管理的开发和生产服务器时,它可以连接而不需要我更改任何内容吗?我正在使用application.yml文件来配置spring => datasource => jndi-name。

谢谢

英文:

So I was curious if anyone has a pattern out there to solve this with a recent version of Spring Boot? My IT Department hosts their own Tomcat servers and prior to me switching over to Spring Boot we would create our own DataSource configuration class to get the proper database connection information from the servers Context.xml because IT managed their own database credentials and just gave us the JNDI path. Then when developing locally would use a local Context.XML to do the same thing with the same JNDI path but using my issued credentials. Can I do this with embedded tomcat so when I deploy to Dev and Production servers managed by IT that it will connect without me having to change anything? I am using application.yml file to configure spring=>datasource=>jndi-name.

Thanks

答案1

得分: 0

这是Spring Profiles的一个完美使用案例。

您可以以多种方式使用配置文件。
请参阅此文章以获取概述
对于您的用例,以下步骤可能足够了。

  1. 创建生产环境下的 application.properties 文件,并按正常方式配置JNDI数据源的配置。
  2. 创建该文件的副本,命名为 application-dev.properties,然后将数据源配置替换为本地数据源的配置。
  3. 配置您的开发环境以设置 spring.profiles.active=dev

这本质上是M. Deinum的评论。所以,如果您觉得它有用,请找到他的一些不错的回答并为其点赞。应该不难找到一个好的回答。

英文:

This is a perfect use case for Spring Profiles.

You can work with profiles in many ways.
See this article for an overview.
For your use case the following is probably sufficient.

  1. Create the production application.properties with the configuration for the JNDI datasource as normal.
  2. Create a copy of that file as application-dev.properties, and replace the datasource configuration with the configuration of your local datasource.
  3. Configure your development environment to set spring.profiles.active=dev

This is essentially the comment of M. Deinum. So if you find it useful find some nice answer of him and give it an upvote. Shouldn't be difficult to find a good one.

huangapple
  • 本文由 发表于 2023年7月7日 04:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定