Suz*_*ioc 155 java spring tomcat jndi datasource
据说在关于DriverManagerDataSource类的Spring javadoc文章中,这个类非常简单并且推荐使用
使用容器提供的JNDI DataSource.这样的一个
DataSource可以DataSource在Spring ApplicationContext via中作为bean 公开JndiObjectFactoryBean
问题是:我该如何做到这一点?
例如,如果我希望有DataSourcebean来访问我的自定义MySQL数据库,那么我需要什么呢?我应该在上下文配置等中写什么?
kal*_*ech 299
如果使用Spring的基于XML模式的配置,请在Spring上下文中进行设置,如下所示:
<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" />
Run Code Online (Sandbox Code Playgroud)
或者,使用这样的简单bean配置进行设置:
<bean id="DatabaseName" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
您可以使用以下内容在tomcat的server.xml中声明JNDI资源:
<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>
Run Code Online (Sandbox Code Playgroud)
并从Tomcat的web context.xml引用JNDI资源,如下所示:
<ResourceLink name="jdbc/DatabaseName"
global="jdbc/DatabaseName"
type="javax.sql.DataSource"/>
Run Code Online (Sandbox Code Playgroud)
参考文件:
编辑:已针对Tomcat 8和Spring 4更新了此答案.对Tomcat的默认数据源资源池设置进行了一些属性名称更改.
Abd*_*ull 50
使用Spring的JavaConfig机制,您可以这样做:
@Configuration
public class MainConfig {
...
@Bean
DataSource dataSource() {
DataSource dataSource = null;
JndiTemplate jndi = new JndiTemplate();
try {
dataSource = jndi.lookup("java:comp/env/jdbc/yourname", DataSource.class);
} catch (NamingException e) {
logger.error("NamingException for java:comp/env/jdbc/yourname", e);
}
return dataSource;
}
}
Run Code Online (Sandbox Code Playgroud)
mel*_*lik 20
假设您在tomcat配置中有一个"sampleDS"数据源定义,您可以添加以下行来applicationContext.xml使用JNDI访问数据源.
<jee:jndi-lookup expected-type="javax.sql.DataSource" id="springBeanIdForSampleDS" jndi-name="sampleDS"/>
Run Code Online (Sandbox Code Playgroud)
您必须jee使用以下内容为前缀定义名称空间和模式位置:
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
Run Code Online (Sandbox Code Playgroud)
ska*_*man 15
文档:C.2.3.1 <jee:jndi-lookup/>(简单)
例:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource"/>
Run Code Online (Sandbox Code Playgroud)
您只需要找出您的appserver绑定数据源的JNDI名称.这完全是服务器特定的,请查阅服务器上的文档以了解具体方法.
请记住jee在bean文件的顶部声明命名空间,如C.2.3 jee架构中所述.
小智 8
另一个特性:您可以在
your_application/META-INF/Context.xml(根据tomcat文档)中添加"Resource"标签,而不是server.xml,如下所示:
<Context>
<Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
username="dbUsername" password="dbPasswd"
url="jdbc:postgresql://localhost/dbname"
driverClassName="org.postgresql.Driver"
initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5"
validationQuery="select 1"
poolPreparedStatements="true"/>
</Context>
Run Code Online (Sandbox Code Playgroud)
小智 5
根据 Apache Tomcat 7 JNDI Datasource HOW-TO页面,web.xml中必须有资源配置:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
Run Code Online (Sandbox Code Playgroud)
这对我行得通
| 归档时间: |
|
| 查看次数: |
252660 次 |
| 最近记录: |