spring配置jndi_在Spring Framework中通过JNDI进行配置

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.xsd
     http://www.springframework.org/schema/context
         http://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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值