javascript
spring配置jndi_在Spring Framework中通过JNDI进行配置
spring配置jndi
從某個(gè)時(shí)候開始,應(yīng)用程序必須是可配置的。 從第一個(gè)版本0.9開始,Spring Framework就為該問題提供了一個(gè)很好的輔助工具,該類為PropertyPlaceholderConfigurer類,而從Spring Framework 3.1開始,為PropertySourcesPlaceholderConfigurer類。 在Google上搜索PropertyPlaceholderConfigurer時(shí),您會(huì)發(fā)現(xiàn)許多示例,這些示例中的配置項(xiàng)保存在屬性文件中。 但是在許多Java企業(yè)應(yīng)用程序中,通常通過JNDI查找加載配置項(xiàng)。 我想演示PropertyPlaceholderConfigurer (在Spring Framework 3.1之前)以及相應(yīng)的PropertySourcesPlaceholderConfigurer (從Spring Framework 3.1開始)如何幫助簡(jiǎn)化在我們的應(yīng)用程序中通過JNDI查找的配置。
初始情況
我們有一個(gè)與數(shù)據(jù)庫連接的Web應(yīng)用程序。 該數(shù)據(jù)庫連接必須是可配置的。 配置項(xiàng)目在Web應(yīng)用程序上下文文件中定義。
context.xml
<Context docBase="/opt/tomcat/warfiles/jndi-sample-war.war" antiResourceLocking="true"><Environment name="username" value="demo" type="java.lang.String" override="false"/><Environment name="password" value="demo" type="java.lang.String" override="false"/>url" value="jdbc:mysql://localhost:3306/wicket_demo" type="java.lang.String" override="false"/> </Context>為了加載這些配置項(xiàng),使用了JNDI查找機(jī)制。
在我們的應(yīng)用程序中,我們?cè)赟pring上下文XML文件中定義了一個(gè)數(shù)據(jù)源bean。 該bean表示數(shù)據(jù)庫連接。
<?xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><!--<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->bean> </beans>啟動(dòng)應(yīng)用程序時(shí),應(yīng)將每個(gè)以$ {}開頭和結(jié)尾的值替換為PropertyPlaceholderConfigurer,并相應(yīng)地使用PropertySourcesPlaceholderConfigurer 。 下一步是設(shè)置PropertyPlaceholderConfigurer,并相應(yīng)地設(shè)置PropertySourcesPlaceholderConfigurer。
在Spring Framework 3.1之前–為JNDI查找設(shè)置
我們?cè)赟pring上下文XML文件中定義了PropertyPlaceholderConfigurer bean。 此bean包含一個(gè)內(nèi)部bean,該內(nèi)部bean將數(shù)據(jù)源bean的屬性名稱映射到相應(yīng)的JNDI名稱。 JNDI名稱由兩部分組成。 第一部分是資源所在的上下文的名稱(在我們的示例中為java:comp / env / ),第二部分是資源的名稱(在我們的示例中為用戶名,密碼或url)。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="properties"><bean class="java.util.Properties"><constructor-arg><map><entry key="username"><jee:jndi-lookup jndi-name="java:comp/env/username" /></entry><entry key="password"><jee:jndi-lookup jndi-name="java:comp/env/password" /></entry><entry key="url"><jee:jndi-lookup jndi-name="java:comp/env/url" /></entry></map></constructor-arg></bean></property> </bean>從Spring Framework 3.1開始–為JNDI查找設(shè)置
既然Spring 3.1 PropertySourcesPlaceholderConfigurer應(yīng)該被用來替代PropertyPlaceholderConfigurer。 這會(huì)影響從Spring 3.1開始,<context:property-placeholder />命名空間元素將注冊(cè)PropertySourcesPlaceholderConfigurer的實(shí)例(命名空間定義必須為spring-context-3.1.xsd),而不是PropertyPlaceholderConfigurer (使用名稱空間定義spring-context-3.0.xsd)。 因此,當(dāng)您遵守某些約定時(shí)(基于約定優(yōu)于配置的原則) ,我們的Spring XML上下文配置非常短。
<context:property-placeholder/>默認(rèn)行為是PropertySourcesPlaceholderConfigurer遍歷一組PropertySource以收集所有屬性值。 在基于Spring的Web應(yīng)用程序中,此集合默認(rèn)包含JndiPropertySource 。 默認(rèn)情況下, JndiPropertySource會(huì)在前綴為java:comp / env的 JNDI資源名稱之后進(jìn)行查找。 這意味著,如果您的屬性為$ {url} ,則相應(yīng)的JNDI資源名稱必須為java:comp / env / url 。
- 該示例Web應(yīng)用程序的源代碼托管在GitHub上 。
翻譯自: https://www.javacodegeeks.com/2015/05/configuration-over-jndi-in-spring-framework.html
spring配置jndi
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的spring配置jndi_在Spring Framework中通过JNDI进行配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AMD 23.9.3 BETA 驱动发布
- 下一篇: redis集成spring_将Redis