使用jndi数据源:
当我们的应用程序运行在J2EE的服务器中的时候,服务程序可以通过配置JNDI来获取数据源-----优势:数据源的管理完全在程序之外管理,程序本身只需要在方未能数据库请求数据源即可。
1、通过JndiObjectFactoryBean获取数据源 普通的bean
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<property name="jndiName" value="/jdbc/rantDataSource"/>
<property name="resourceRef" value="true"/>
</bean>
属性jndiName指JNDI里的数据源的名称
属性resourceRef为true时,jndi会被添加java:comp/env/,从应用程序服务器的JNDI目录中获取数据源,实际的数据源名称为:java:comp/env/jdbc/rantDataSource
2、spring中的jndi配置数据源
引入jee命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
配置数据源
<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/rantDataSource" resource-ref="true"/>