Java 在Spring3.1应用程序xml中访问PropertySources
我正在使用Spring应用程序ContextInitializer将PropertySources添加到环境中。我已经验证了该部分在将属性加载到PropertySources时的工作方式。我正在努力解决的部分是如何访问SpringXML配置中ApplicationContextInitializer中加载的属性值以及Java代码本身 下面是我的ApplicationContextInitializer的一小部分:Java 在Spring3.1应用程序xml中访问PropertySources,java,spring,properties,applicationcontext,Java,Spring,Properties,Applicationcontext,我正在使用Spring应用程序ContextInitializer将PropertySources添加到环境中。我已经验证了该部分在将属性加载到PropertySources时的工作方式。我正在努力解决的部分是如何访问SpringXML配置中ApplicationContextInitializer中加载的属性值以及Java代码本身 下面是我的ApplicationContextInitializer的一小部分: public class ExternalPropertiesApplicatio
public class ExternalPropertiesApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
Properties properties = load_properties_from_external_source();
PropertiesPropertySource propertySource = new PropertiesPropertySource("external", properties);
applicationContext.getEnvironment().getPropertySources().addFirst(propertySource);
}
}
我的Spring配置文件如下所示:
<context:component-scan base-package="com.example"/>
<context:property-placeholder ignore-resource-not-found="true" system-properties-mode="OVERRIDE"/>
<bean id="myds" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
与Spring配置类似,如何访问初始化时加载的jdbc.url属性值
哦,下面是web.xml的一个小片段:
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.example.ExternalPropertiesApplicationContextInitializer</param-value>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring配置文件中有几个问题是罪魁祸首 具体而言,在标题中,我有:
<beans 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
尽管是在Spring3.1上。这导致Spring恢复到旧的PropertyPlaceHolderConfigure,而不是新的PropertySourcesplacePlaceHolderConfigurer
一旦我将3.0.xsd引用更改为3.1.xsd,它就开始解决这个问题。新标题如下所示:
package com.example;
@Component
public class MyClass {
@Value("${jdbc.url}")
private String jdbcUrl;
}
<beans 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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
第二个问题是这一行:
<context:property-placeholder ignore-resource-not-found="true" system-properties-mode="OVERRIDE"/>
由于系统属性模式属性,Spring再次恢复为旧的PropertyPlaceHolderConfigure,而不是新的PropertySourcesPlaceholderConfigurer
使用旧的PropertyPlaceHolderConfigure时,它没有从新环境中获取值,这就是为什么在Spring上下文配置中没有获取属性值。在哪里注册ApplicationContextInitializer?@Sotirios-在web.xml中,例如:contextConfigLocation类路径:applicationContext.xmlcontextInitializerClasses com.example.ExternalPropertiesApplicationContextInitializer org.springframework.web.context.ContextLoaderListener请编辑您的问题并添加到那里。它在注释中不可读。@Sotirios-在您的注释之后,我回去查看我的web.xml,发现我在加载自己的上下文初始值设定项之前加载了Spring配置文件,因此它无法找到它。一旦我改变了顺序,我就可以访问属性文件了。谢谢嗯…没做什么,但不客气。考虑删除这个问题,因为它可能不会帮助任何其他人。
<context:property-placeholder ignore-resource-not-found="true" system-properties-mode="OVERRIDE"/>