JNDI配置数据源(tomcat上配置)
来源:互联网 发布:学霸 知乎 编辑:程序博客网 时间:2024/04/30 20:06
JNDI全称Java Naming and Directory Interface,即Java命名和目录接口,目前我只知道它可以用来配置数据源,至于其它用法和意义可以百度。
JNDI配置数据源的意义:
我感觉它主要意义是降低了数据源与项目的耦合度,数据源在服务器(比如tomcat)上配置,应用程序引用数据源就行。这样多个项目用同一个数据源时就不用一个一个地给项目复制粘贴相同的数据源,也不用导那么多相同的jar包了。
配置数据源步骤:
1、配置server.xml,既然数据源是配置到tomcat上,那就到tomcat安装目录->conf->server.xml里配置数据源,数据源配置在GlobalNamingResources节点中,具体配置如下:
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource
name="mysqlSource"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/tianyashop?useUnicode=true&characterEncoding=utf-8"/>
<Resource
name="postgresqlSource"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="postgres"
password="postgres"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/wsshp/?useUnicode=true&characterEncoding=utf-8"/>
</GlobalNamingResources>
配置说明:
name:表示以后要查找的名称。通过此名称可以找到DataSource
auth:表示认证方式,有Container与application值可选,一般填前者
type:此名称所代表的类型,现在为javax.sql.DataSource
maxActive:表示一个数据库在此服务器上所能打开的最大连接数
maxIdle:表示一个数据库在此服务器上维持的最小连接数
maxWait:最大等待时间。10000毫秒
username:数据库连接的用户名
password:数据库连接的密码
driverClassName:数据库连接的驱动程序
url:数据库连接的地址
2、映射数据源,在tomcat的conf->context.xml中配置数据源映射,将<ResourceLink>配置到<Context>节点中,这样所有项目都可以引用该数据源,具体如下:
<Context>
<ResourceLink global="postgresqlSource" name="postgresqlSource" type="javax.sql.DataSource" />
<ResourceLink global="mysqlSource" name="mysqlSource" type="javax.sql.DataSource" />
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
当然,若不希望所有项目可引用该数据源,可以将ResourceLink写在各自项目的上下文xml文件中,即在tomcat目录的\conf\Catalina\localhost的{要添加tomcat数据源的项目名称}.xml文件中添加ResourceLink节点配置数据源,该节点依旧在Context节点中,具体如下:
<Context path="/tianyashop" reloadable="true" docBase="E:\Java\workspace\tianyashop\src\main\webapp" workDir="E:\Java\workspace\tianyashop\work" >
<!-- Extra info begin -->
<ResourceLink global="postgresqlSource" name="postgresqlSource" type="javax.sql.DataSource" />
<ResourceLink global="mysqlSource" name="mysqlSource" type="javax.sql.DataSource" />
<!-- Extra info end -->
<Logger className="org.apache.catalina.logger.SystemOutLogger" verbosity="4" timestamp="true"/>
<Loader className="org.apache.catalina.loader.DevLoader" reloadable="true" debug="1" useSystemClassLoaderAsParent="false" />
</Context>
3、添加数据库驱动包,将上面配置的数据库的驱动包添加在tomcat的lib文件夹中,例如mysql的mysql-connector-java-5.x.jar添加到tomcat的lib文件夹下。
4、在项目中配置数据源,tomcat配置好后,接着到项目中配置,若用spring管理数据源的话直接在spring的配置文件中(一般是rootContext.xml或applicationContext.xml)添加数据源,具体如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 根据扫描包中注解的类 -->
<context:component-scan base-package="com.tianyashop" />
<!-- jndi配置数据源,连接池在tomcat的conf目录下server.xml文件中,jee用了http://www.springframework.org/schema/jee/spring-jee.xsd的命名空间 -->
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/mysqlSource" resource-ref="true" />
</beans>
注意,这里可以写jee标签是声明了spring-jee命名空间的缘故,也不用额外导什么包,除了上面这个声明数据源,也可以用bean标签,具体如下:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/mysqlSource</value>
</property>
</bean>
好了,到此JNDI配置数据源就好了,spring的dataSource就可以注入到各个bean中了。
- JNDI配置数据源(tomcat上配置)
- tomcat配置数据源 (JNDI)
- tomcat JNDI数据源配置
- Tomcat 配置JNDI 数据源
- tomcat JNDI数据源配置
- Tomcat配置JNDI数据源
- tomcat JNDI配置数据源
- Tomcat配置JNDI数据源
- Tomcat配置JNDI数据源
- tomcat配置JNDI数据源
- Tomcat配置JNDI数据源
- Tomcat配置JNDI数据源
- tomcat配置jndi数据源
- Tomcat配置JNDI数据源
- Tomcat配置JNDI数据源
- Tomcat配置JNDI数据源
- tomcat 配置JNDI 数据源
- tomcat配置JNDI数据源
- 《Spring技术内幕》学习笔记6——IoC容器的高级特性
- Qt5 QToolTip 背景色 背景样式
- spring security起步二:自定义登录页
- 切换Win10系统语言环境的方法
- dedecms 5.7版本已增加按权重排序功能
- JNDI配置数据源(tomcat上配置)
- Java实例化对象的几种方式
- Java集合类知识整理
- H5判断网络状态
- 一个C源文件到可执行文件 [反汇编-函数栈帧 编译 链接]
- MySQL系列之架构浅析
- js 阿拉伯数字转中文大写货币
- 行人属性“Actions and Attributes from Wholes and Parts”
- Tapestry(二):Tapestry基本知识