简体   繁体   English

如何使用Java配置在Tomcat 8中配置JNDI DataSource:

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

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>

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”文件,如下图所示:

在此输入图像描述

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上下文配置文件中创建以下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”属性。

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”配置文件,对于单元测试,您需要具有与Web服务器无关的数据源)

<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>

It can be done in 3 steps: 它可以分3个步骤完成:

Step 1: 第1步:

Add below entry in tomcat conf/server.xml under GlobalNamingResources tag. GlobalNamingResources标记下的tomcat conf / server.xml中添加以下条目。

  <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 context tag. 在根上下文标记下的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中添加DataSource引用

<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可以与不同的应用程序共享。

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

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