如何使用 Java 配置在 Tomcat 8 中配置 JNDI 数据源:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32957132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:01:26  来源:igfitidea点击:

How to Configure JNDI DataSource in Tomcat 8 with Java Configuration:

javaspringjndi

提问by samadadi

How to Configure JNDI DataSource in Java Configuration File Instead of Following Code Snippet in "web.xml" Servlet Context:

如何在 Java 配置文件中配置 JNDI 数据源,而不是遵循“web.xml”Servlet 上下文中的代码片段:

<resource-ref>
   <description>DB Connection</description>
   <res-ref-name>jdbc/DatabaseName</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

回答by samadadi

Note: Don't Forget to Copy the "mysql-connector-java-5.1.36.jar" Into Tomcat's "lib" Subfolder in Main Installation Folder.

注意:不要忘记将“mysql-connector-java-5.1.36.jar”复制到Tomcat主安装文件夹中的“lib”子文件夹中。

First: Add following Dependency in Your "pom.xml" File:

首先:在“pom.xml”文件中添加以下依赖项:

 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.36</version>
 </dependency>

Second: Create META-INF Folder and "context.xml" File in "webapp" Root Folder Like the Following Picture:

第二:在“webapp”根文件夹中创建META-INF文件夹和“context.xml”文件,如下图:

enter image description here

在此处输入图片说明

Third: Add the Following Code Snippet in "context.xml" File:

第三:在“context.xml”文件中添加以下代码片段:

<?xml version='1.0' encoding='utf-8'?>

<Context>
    <Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
              maxActive="50" maxIdle="30" maxWait="10000"
              username="DatabaseUsername" password="DatabasePasssword"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/DatabaseName"/>
</Context>

Fourth: Create the Following Bean in Spring Context Configuration File:

第四:在Spring Context配置文件中创建如下Bean:

@Bean
public DataSource dataSource() {
    JndiDataSourceLookup dataSource = new JndiDataSourceLookup();
    dataSource.setResourceRef(true);
    return dataSource.getDataSource("jdbc/DatabaseName");
}

Note: "jdbc/DatabaseName" is "name" Attribute that We Added Already in "context.xml" File.

注意:“jdbc/DatabaseName”是我们已经在“context.xml”文件中添加的“name”属性。

回答by Raphael Roth

To complete SMGs answer: for xml-configured Spring, I use the following code (note the "webapp" profile, as for unit-tests you need to have a webserver-independent datasource)

要完成 SMG 的回答:对于 xml 配置的 Spring,我使用以下代码(注意“webapp”配置文件,至于单元测试,您需要有一个独立于网络服务器的数据源)

<beans profile="webapp">
    <!-- get dataSources from web-container -->
    <bean name="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
        <property name="jndiName" value="java:comp/env/jdbc/DatabaseName" />
        <property name="resourceRef" value="true" />
    </bean>
</beans>

回答by Prasobh.Kollattu

It can be done in 3 steps:

它可以通过 3 个步骤完成:

Step 1:

步骤1:

Add below entry in tomcat conf/server.xml under GlobalNamingResourcestag.

在 tomcat conf/server.xml 中GlobalNamingResources标签下添加以下条目。

  <Resource auth="Container" driverClassName="DB_Drive_class-name" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/MyJNDI" password="&&&&&&&&&&&&&" type="javax.sql.DataSource" url="jdbc:db2://URL:PORT/DBNAME" username="&&&&&&&&&"/>

Step 2:

第2步:

Add below entry in tomcat conf/context.xml under root contexttag.

在根上下文标记下的 tomcat conf/context.xml 中添加以下条目。

<ResourceLink  name="jdbc/MyJNDI"  global="jdbc/MyJNDI" type="javax.sql.DataSource"/>

Step 3:

第 3 步:

Add DataSource ref in web.xml

web.xml 中添加数据源引用

<resource-ref>
 <description>DB2 Datasource</description>
 <res-ref-name>jdbc/MyJNDI</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

Note:This is a global Tomcat configuration and DataSource can be shared with different applications.

注意:这是一个全局的 Tomcat 配置,DataSource 可以与不同的应用程序共享。