Spring中如何使用Tomcat提供的JNDI数据源? [英] How to use JNDI DataSource provided by Tomcat in Spring?

查看:43
本文介绍了Spring中如何使用Tomcat提供的JNDI数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring javadoc的文章中说DriverManagerDataSource类,这个类很简单,推荐使用

It is said that in the Spring javadoc article about DriverManagerDataSource class, that this class is very simple and that it is recommended

使用容器提供的 JNDI 数据源.这样的 DataSource 可以通过 JndiObjectFactoryBean

to use a JNDI DataSource provided by the container. Such a DataSource can be exposed as a DataSource bean in a Spring ApplicationContext via JndiObjectFactoryBean

问题是:我该如何做到这一点?

例如,如果我希望有 DataSource bean 来访问我的自定义 MySQL 数据库,那么我需要什么?我应该在上下文配置等中写什么?

For example, if I wish to have DataSource bean to access my custom MySQL database, what would I require then? What should I write in the context configuration, etc?

推荐答案

如果使用 Spring 的基于 XML 模式的配置,在 Spring 上下文中设置如下:

If using Spring's XML schema based configuration, setup in the Spring context like this:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
...
<jee:jndi-lookup id="dbDataSource"
   jndi-name="jdbc/DatabaseName"
   expected-type="javax.sql.DataSource" />

或者,使用简单的 bean 配置进行设置,如下所示:

Alternatively, setup using simple bean configuration like this:

<bean id="DatabaseName" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>

您可以使用以下内容在 tomcat 的 server.xml 中声明 JNDI 资源:

You can declare the JNDI resource in tomcat's server.xml using something like this:

<GlobalNamingResources>
    <Resource name="jdbc/DatabaseName"
              auth="Container"
              type="javax.sql.DataSource"
              username="dbUser"
              password="dbPassword"
              url="jdbc:postgresql://localhost/dbname"
              driverClassName="org.postgresql.Driver"
              initialSize="20"
              maxWaitMillis="15000"
              maxTotal="75"
              maxIdle="20"
              maxAge="7200000"
              testOnBorrow="true"
              validationQuery="select 1"
              />
</GlobalNamingResources>

并像这样从 Tomcat 的 web context.xml 引用 JNDI 资源:

And reference the JNDI resource from Tomcat's web context.xml like this:

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

参考文档:

此答案已针对 Tomcat 8 和 Spring 4 更新.Tomcat 的默认数据源资源池设置有一些属性名称更改.

This answer has been updated for Tomcat 8 and Spring 4. There have been a few property name changes for Tomcat's default datasource resource pool setup.

这篇关于Spring中如何使用Tomcat提供的JNDI数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆