概念:
JNDI:java name description interface java命名與目錄接口,是一組在Java應用中訪問命名和目錄服務的API。命名服務將名稱和對象聯繫起來,使得我們可以用名稱
訪問對象。目錄服務是一種命名服務,在這種服務裏,對象不但有名稱,還有屬性。
配置:
tomcat配置jndi有全局配置和局部配置
全局配置:(推薦:可以適用於其他項目)
第一步:在tomcat安裝目錄下conf/context.xml文件配置數據源
<context>
<Resource
auth="Container" driverClassName="org.postgresql.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
<!-- 配置jdni name屬性對應datasource中的value-->
name="xds-pg"
password="paic1234"
type="javax.sql.DataSource" url="jdbc:postgresql://10.20.129.56:7589/d4paebd" username="pieropr"/>
</context>
第二步:spring配置文件datasource
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="true">
<!-- tomcat配置文件中配置的resource的name值-->
<property name="jndiName" value="xds-pg" />
<property name="resourceRef" value="true" />
</bean>
局部配置:
局部配置。
第一步:在項目的META-INFO下面新建context.xml。加入:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jndi/mybatis"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/appdb"
username="root"
password="123456"
maxActive="20"
maxIdle="10"
maxWait="10000"/>
</Context>
第二步:配置datasource