简体   繁体   English

在Spring Boot应用程序中使用JNDI的任何理由?

[英]Any reason for JNDI in a Spring Boot app?

Background 背景

We are converting a traditional java application from a war deployed to a tomcat container into a Spring Boot application that runs in an embedded tomcat server. 我们正在将传统的Java应用程序从战争部署到tomcat容器中转换为运行在嵌入式tomcat服务器中的Spring Boot应用程序。 We are currently using JNDI for the database connections which are defined in the context.xml in the conf/ folder in the tomcat instance. 我们当前正在使用JNDI进行数据库连接,这些连接在tomcat实例的conf/文件夹中的context.xml中定义。

Problem 问题

There's not a way to do this with a Spring Boot application, not exactly that is. 使用Spring Boot应用程序无法做到这一点,事实并非如此。 I have found solutions to define the data source with JNDI in a Spring Boot application but it requires all the connection information in the code. 我已经找到了在Spring Boot应用程序中使用JNDI定义数据源的解决方案,但是它需要代码中的所有连接信息。 This got me thinking, if the connection info is in the code, is there even a point to be using JNDI? 这让我开始思考, 如果连接信息在代码中,是否还有使用JNDI的意义? I thought the point of JNDI was to separate the details of how to connect so that it could be defined per environment. 我认为JNDI的重点是分开如何连接的细节,以便可以针对每个环境进行定义。

Alternative 替代

Define a data source without JNDI and have the connection details be defined externally (eg, command line params, Spring Cloud Config Server, or something else). 定义不带JNDI的数据源,并在外部定义连接详细信息(例如,命令行参数,Spring Cloud Config Server或其他)。

Keep in mind that JNDI was originally conceived as a way to abstract away the specific connection details of a datasource (well, not just datasources, but you get the gist). 请记住,JNDI最初被认为是一种抽象数据源特定连接细节的方法(当然,不仅是数据源,而是您的要旨)。 This abstractin makes sense in general, so JNDI was an officially sanctioned standard that had to be implemented in everything that wanted to be JEE (J2EE? can't remember) compatible. 一般而言,这种抽象是有道理的,因此JNDI是官方认可的标准,必须在所有想要与JEE(J2EE?不记得了)兼容的东西中实现。

You still need a configuration abstraction, as you don't want to hardcode all the values for all your different environments in code, but Spring has ready-made solutions for that. 您仍然需要配置抽象,因为您不想在代码中对所有不同环境的所有值进行硬编码,但是Spring为此提供了现成的解决方案。 Personally I wouldn't bother with JNDI in anything that would never be in an JEE server. 就个人而言,我不会在JEE服务器中永远不会遇到的JNDI麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM