如何在不同的上下文中使用多个property-placeholder

时间:2015-03-24 08:41:20

标签: java spring spring-security properties-file

我使用Spring Framework和Spring Security。我需要使用页面路径的不同位置。例如," / mainpath"。 我把它放在/WEB-INF/classes/path.properties中。 该路径应该在servlet-context.xml和security-context.xml中使用。 如果在两个上下文中都放置了以下内容

<context:property-placeholder location="/WEB-INF/classes/path.properties"/>

将导致

java.lang.IllegalArgumentException: "Could not resolve placeholder 'main.page' in string value "${main.page}"

以下内容未给出必要的结果。

<context:property-placeholder location="/WEB-INF/classes/path.properties" ignore-unresolvable="true"/>

UPD。 path.properties:

main.page=mainpage

我从servlet-context.xml中排除了property-placeholder。错误仍然存​​在。 安全上下文的一部分:

<context:property-placeholder location="classpath:/path.properties" 
    ignore-unresolvable="false"/>
<http use-expressions="true">
    <intercept-url pattern="/${main.page}" access="isAuthenticated()" 
    requires-channel="http"/>
...
</http>

我尝试将path.properties放在不同的文件夹中。 Property-placeholder还在datasource-tx-jpa.xml中与其他属性文件一起使用。

文件夹结构:

Folders structure

堆栈跟踪:

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0' defined in null: Could not resolve placeholder 'main.page' in string value "/${main.page}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'main.page' in string value "/${main.page}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:155)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4720)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'main.page' in string value "/${main.page}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:194)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:158)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:209)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitIndexedArgumentValues(BeanDefinitionVisitor.java:150)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:84)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:169)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitMap(BeanDefinitionVisitor.java:259)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:198)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitIndexedArgumentValues(BeanDefinitionVisitor.java:150)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:84)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:169)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208)
    ... 18 more

UPD 2。 问题出在datasource-tx-jpa.xml中的property-placeholder中,它在security-context.xml之前的root-context.xml中导入。因此,它无法在与datasource-tx-jpa.xml关联的属性文件中找到占位符。 属性&#34; ignore-unresolvable&#34;属性占位符应在第一个导入的上下文中设置为true。

2 个答案:

答案 0 :(得分:1)

将文件位置定义为classpath 似乎不是一个好名字,如果您使用maven将您的文件放在资源文件夹下,并写下您的文件property-placeholder as

<context:property-placeholder location="classpath:/path.properties" />

如果您不使用maven在配置中定义文件夹,则在您的配置中为类(我建议重命名),在此选项卡中定义此文件夹Build Path->Configure Build Path->Source Add classes文件夹

答案 1 :(得分:0)

问题出在datasource-tx-jpa.xml中的property-placeholder,它在security-context.xml之前的root-context.xml中导入。因此,它无法在与datasource-tx-jpa.xml关联的属性文件中找到占位符。属性&#34; ignore-unresolvable&#34;属性占位符应在第一个导入的上下文中设置为true。