javascript
在Spring Framework中通过JNDI进行配置
從某個時候開始,應用程序必須是可配置的。 自第一個版本0.9起,Spring Framework就為該問題提供了一個很好的輔助工具,該類為PropertyPlaceholderConfigurer類,而從Spring Framework 3.1起為PropertySourcesPlaceholderConfigurer類。 在Google上搜索PropertyPlaceholderConfigurer時,您會發現許多示例,這些示例中的配置項保存在屬性文件中。 但是在許多Java企業應用程序中,通常通過JNDI查找加載配置項。 我想演示PropertyPlaceholderConfigurer (在Spring Framework 3.1之前)以及相應的PropertySourcesPlaceholderConfigurer (從Spring Framework 3.1開始)如何幫助簡化在我們的應用程序中通過JNDI查找的配置。
初始情況
我們有一個與數據庫連接的Web應用程序。 該數據庫連接必須是可配置的。 配置項目在Web應用程序上下文文件中定義。
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>為了加載這些配置項,使用了JNDI查找機制。
在我們的應用程序中,我們在Spring上下文XML文件中定義了一個數據源bean。 該bean代表數據庫連接。
<?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>啟動應用程序時,應將每個以$ {}開頭和結尾的值替換為PropertyPlaceholderConfigurer,并相應地使用PropertySourcesPlaceholderConfigurer 。 下一步是設置PropertyPlaceholderConfigurer,并相應地設置PropertySourcesPlaceholderConfigurer。
在Spring Framework 3.1之前–為JNDI查找設置
我們在Spring上下文XML文件中定義了PropertyPlaceholderConfigurer bean。 此bean包含一個內部bean,該內部bean將數據源bean的屬性名稱映射到相應的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查找設置
既然Spring 3.1 PropertySourcesPlaceholderConfigurer應該被用來替代PropertyPlaceholderConfigurer。 這會影響從Spring 3.1開始,<context:property-placeholder />命名空間元素將注冊PropertySourcesPlaceholderConfigurer (命名空間定義必須為spring-context-3.1.xsd)的實例,而不是PropertyPlaceholderConfigurer (您可以在使用名稱空間定義spring-context-3.0.xsd)。 因此,當您遵守某些約定時(基于約定優于配置的原則) ,我們的Spring XML上下文配置非常短。
<context:property-placeholder/>默認行為是PropertySourcesPlaceholderConfigurer遍歷一組PropertySource以收集所有屬性值。 在基于Spring的Web應用程序中,此集合默認包含JndiPropertySource 。 默認情況下, JndiPropertySource在以java:comp / env為前綴的JNDI資源名稱之后進行查找。 這意味著,如果您的屬性為$ {url} ,則相應的JNDI資源名稱必須為java:comp / env / url 。
- 該示例Web應用程序的源代碼托管在GitHub上 。
翻譯自: https://www.javacodegeeks.com/2015/05/configuration-over-jndi-in-spring-framework.html
總結
以上是生活随笔為你收集整理的在Spring Framework中通过JNDI进行配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 金蝶系统提示未设置本利润(金蝶k3中,在
- 下一篇: 中国戏剧主要包括哪二类(戏剧戏曲曲艺有什