服务器建网站教程,wordpress 没关插件,镇江大港属于哪个区,郑州商标设计公司spring配置jndi从某个时候开始#xff0c;应用程序必须是可配置的。 从第一个版本0.9开始#xff0c;Spring Framework就为该问题提供了一个很好的辅助工具#xff0c;该类为PropertyPlaceholderConfigurer类#xff0c;而从Spring Framework 3.1开始#xff0c;为Propert… 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 antiResourceLockingtrueEnvironment nameusername valuedemo typejava.lang.String overridefalse/Environment namepassword valuedemo typejava.lang.String overridefalse/url valuejdbc:mysql://localhost:3306/wicket_demo typejava.lang.String overridefalse/
/Context 为了加载这些配置项使用了JNDI查找机制。 在我们的应用程序中我们在Spring上下文XML文件中定义了一个数据源bean。 该bean表示数据库连接。 ?xml version1.0 encodingUTF-8?
xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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.xsdbean iddataSource classorg.apache.commons.dbcp.BasicDataSourcedestroy-methodcloseurl value${url} /property nameusername value${username} /property namepassword value${password} /!--span classhiddenSpellError pre data-mce-bogus1--bean
/beans 启动应用程序时应将每个以$ {}开头和结尾的值替换为PropertyPlaceholderConfigurer并相应地使用PropertySourcesPlaceholderConfigurer 。 下一步是设置PropertyPlaceholderConfigurer并相应地设置PropertySourcesPlaceholderConfigurer。 在Spring Framework 3.1之前–为JNDI查找设置 我们在Spring上下文XML文件中定义了PropertyPlaceholderConfigurer bean。 此bean包含一个内部bean该内部bean将数据源bean的属性名称映射到相应的JNDI名称。 JNDI名称由两部分组成。 第一部分是资源所在的上下文的名称在我们的示例中为javacomp / env / 第二部分是资源的名称在我们的示例中为用户名密码或url。 bean idpropertyConfigurer classorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namepropertiesbean classjava.util.Propertiesconstructor-argmapentry keyusernamejee:jndi-lookup jndi-namejava:comp/env/username //entryentry keypasswordjee:jndi-lookup jndi-namejava:comp/env/password //entryentry keyurljee:jndi-lookup jndi-namejava:comp/env/url //entry/map/constructor-arg/bean/property
/bean从Spring Framework 3.1开始–为JNDI查找设置 既然Spring 3.1 PropertySourcesPlaceholderConfigurer应该被用来替代PropertyPlaceholderConfigurer。 这会影响从Spring 3.1开始contextproperty-placeholder /命名空间元素将注册PropertySourcesPlaceholderConfigurer的实例命名空间定义必须为spring-context-3.1.xsd而不是PropertyPlaceholderConfigurer 使用名称空间定义spring-context-3.0.xsd。 因此当您遵守某些约定时基于约定优于配置的原则 我们的Spring XML上下文配置非常短。 context:property-placeholder/ 默认行为是PropertySourcesPlaceholderConfigurer遍历一组PropertySource以收集所有属性值。 在基于Spring的Web应用程序中此集合默认包含JndiPropertySource 。 默认情况下 JndiPropertySource会在前缀为javacomp / env的 JNDI资源名称之后进行查找。 这意味着如果您的属性为$ {url} 则相应的JNDI资源名称必须为javacomp / env / url 。 该示例Web应用程序的源代码托管在GitHub上 。 翻译自: https://www.javacodegeeks.com/2015/05/configuration-over-jndi-in-spring-framework.htmlspring配置jndi