JNDI就是Sun提出的一套对象命名和目录服务的接口,全称为Java Naming and Directory Interface
,简单的说就是JNDI经过目录服务的基础上抽象了一层来查找Java对象。引用维基百科中的定义以下:html
The Java Naming and Directory Interface ( JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects)) via a name. Like all Java) APIs that interface with host systems, JNDI is independent of the underlying implementation. Additionally, it specifies a service provider interface (SPI) that allows directory service implementations to be plugged into the framework. [1] The information looked up via JNDI may be supplied by a server, a flat file, or a database; the choice is up to the implementation used.
根据他的定义,能够知道JNDI是经过SPI做为插件的方式应用于框架当中,经过JNDI查找的对象能够经过服务器,文件,或者数据库提供,这个是取决于具体的实现便可。java
在没有JNDI的时候,好比须要链接一个Mysql数据库,则须要经过硬编码的方式达到链接数据库的目的。以下代码所示,mysql
Class.forName("com.mysql.jdbc.Driver",true,Thread.currentThread().getContextClassLoader()); Connection conn = DriverManager.getConnection("jdbc:mysql://test?user=landy&password=123456");
这样作的状况,就是等到需求改变的时候不容易修改,好比服务名称,数据库用户名密码等,甚者链接池参数等均可能修改。程序员
有了JNDI之后,程序员就只要关心本身的实现便可,不须要关注具体的数据库如何链接,如何配置用户名密码等操做。主要应用场景根据维基百科能够知道有如下几种:web
- Connecting a Java application to an external directory service (such as an address database or an LDAP server)
- Allowing a Java Servlet to look up configuration information provided by the hosting web container.
总结起来就是两点,一点是链接数据库或者LDAP Server,第二个就是容许Java Servlet寻找Web容器提供的配置信息,其实这点就至关于我能够把数据库的链接配置信息也配置在Servlet 容器中,达到开发人员与运维人员解耦的要求。spring
Tomcat配置JNDI主要是配置server.xml和context.xml,主要有三种方式配置,能够参考文章tomcat下jndi的三种配置方式 ,本文采用他的全局配置方式,可是有点区别,笔者应用于真正的生产环境既是此种方式。我采用了独立于真正的tomcat容器,而后经过脚本指向tomcat,而后启动便可。sql
根据tomcat官网介绍,Tomcat提供了一套与Java EE标准兼容的模式为其下运行的每一个Web应用程序提供JNDI InitialContext实现实例,而后Java EE标准在WEB-INF/web.xml中定义了一组标准的元素用于引用或者定义相应的资源。数据库
Tomcat provides a JNDI
InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a
Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the
/WEB-INF/web.xml
file to reference/define resources.
其实咱们能够把tomcat的各个目录拷贝到另一个目录下,好比以下目录所示:apache
bin目录主要一个start.bat的批处理文件,conf目录下就把全部的原有tomcat/conf目录下的文件拷贝至此便可,而后修改server.xml和context.xml,至于logs、webapps、work目录做为空目录存在便可。segmentfault
本配置文件主要是配置JVM启动参数和引导启动tomcat容器,配置以下:
set "JAVA_HOME=C:\01_soft\java\jdk1.8.0_202" set "CATALINA_HOME=C:\05_webserver\apache-tomcat-8.5.45" set "CATALINA_BASE=C:\03_code\idea_workspace\spring-boot-lesson\tomcat-instances\tomcat-jndi" set "TITLE=Tomcat JNDI Demo" SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7776 set "JAVA_OPTS=%JAVA_OPTS% -Dlog.path=C:\03_code\idea_workspace\spring-boot-lesson\tomcat-instances\tomcat-jndi\logs -server -Xms1024m -Xmx1024m" call "%CATALINA_HOME%\bin\startup.bat"
如上,配置了JAVA_HOME,CATALINA_HOME,CATALINA_BASE,CATALINA_OPTS,JAVA_OPTS等参数,最后经过call命令调用外部tomcat容器启动tomcat。
transport=dt_socket,server=y,suspend=n,address=7776
。每一个WEB应用程序的初使化环境(InitialContext)能够配置于$CATALINA_HOME/conf/server.xml的<Context>节点中,也能够配置每一个WEB应用程序环境(Context)于单独的XML文件中。
每一个须要JNDI访问的context节点能够配置以下节点,
本文配置一个简单的ResourceLink,而后经过server.xml中引用便可。
<ResourceLink name="jdbc/test" type="javax.sql.DataSource" global="jdbc/test"/>
其中name属性jdbc/test
要与server.xml中Resources节点的name属性名称一致。
参考连接:https://tomcat.apache.org/tom...
Tomcat为整个tomcat服务器维护了一个全局资源的命名空间,它们配置在conf/server.xml文件下的GlobalNamingResources
节点中。你能够用ResourcLink
节点引入每一个web应用程序的上下文来暴露它们的资源在相应的web应用中。
Tomcat maintains a separate namespace of global resources for the entire server. These are configured in the****
element of$CATALINA_BASE/conf/server.xml
. You may expose these resources to web applications by using a []( https://tomcat.apache.org/tom... to include it in the per-web-application context.
本文配置以下:
<Resource name="jdbc/test" auth="Container" loginTimeout="10" maxWait="5000" maxAge="580000" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT" driverClassName="com.mysql.cj.jdbc.Driver" username="root" password="landy8530" />
Resource标签属性:
URL属性的说明以下:
对于MySQL5.*及以前版本只需写
jdbc:mysql://127.0.0.1:3306/databaseName
就行,MySQL6.0及以后版本须要指定服务器时区属性,设定useSSL属性等,个属性之间用&链接,在xml/html文件中&用&
转义表示,应该写成:jdbc:mysql://127.0.0.1:3306/databaseName?serverTimeZone=GMT%2B8&useSSL=false
若出现字符集问题则需添加下面两个参数:
- useUnicode=true
- characterEncoding=utf8
Mysql时区异常信息以下:
java.sql.SQLException: The server time zone value‘XXXXXX' is unrecognized or represents...
参考连接:https://tomcat.apache.org/tom...
本文采用的是Mysql8.0,如何安装能够参考笔者以前的一篇文章基于Windows 10安装Mysql 8.0.17。
Engine配置比较简单,只要配置一个包含访问日志节点Value
和引用上下文节点Context
节点的Host
节点便可。
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".log"/> <Context crossContext="true" docBase="C:\\03_code\\idea_workspace\\spring-boot-lesson\\spring-boot-1.x\\tomcat-jndi-demo\\target\\tomcat-jndi-demo" path="/jndi-demo" reloadable="true"/> </Host>
Context
中的docBase
属性指向真正的web应用的根目录,path
为web应用的根路径或者叫作应用名称也能够。
如下节点能够配置每一个web应用中的web应用描述符(/WEB-INF/web.xml
)定义资源。
Environment entry
),一个单值参数能够用于配置应用如何操做。Resource reference
),典型的对象工厂为JDBC DataSource
, JavaMail Session
等,也能够自定义对象工厂做为资源引进tomcat中。Resource environment reference
),在servlet2.4中引入的新的<resource-ref>的校验,这种能够简化配置,不须要校验信息。本文配置以下:
<resource-ref> <res-ref-name>jdbc/test</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
参考连接:https://tomcat.apache.org/tom...
使用Java读取JNDI配置就很简单了,只要引用JNDI的InitialContext
对象便可读取。主要逻辑以下:
Context context = new InitialContext(); Context evnContext = (Context) context.lookup("java:comp/env"); dataSource = (DataSource) evnContext.lookup("jdbc/test");
获得了DataSource数据源就能够获得数据库链接对象了,
Connection connection = dataSource.getConnection();
启动tomcat主要运行上文配置的startup.bat
批处理文件便可,而后输入地址http://localhost:8080/jndi-demo/jdbc/test
便可访问到本文的测试案例了。