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
便可訪問到本文的測試案例了。