spring配置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配置jndi