首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不同上下文中使用多个属性占位符

如何在不同上下文中使用多个属性占位符
EN

Stack Overflow用户
提问于 2015-03-24 16:41:20
回答 2查看 985关注 0票数 1

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

代码语言:javascript
复制
<context:property-placeholder location="/WEB-INF/classes/path.properties"/>

它将导致

代码语言:javascript
复制
java.lang.IllegalArgumentException: "Could not resolve placeholder 'main.page' in string value "${main.page}"

以下内容没有给出所需的结果。

代码语言:javascript
复制
<context:property-placeholder location="/WEB-INF/classes/path.properties" ignore-unresolvable="true"/>

UPD。path.properties:

代码语言:javascript
复制
main.page=mainpage

我从servlet-context.xml中排除了property-placeholder。错误仍然会出现。security-context部分:

代码语言:javascript
复制
<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放在不同的文件夹中。属性占位符也用于datasource-tx-jpa.xml和其他属性文件。

文件夹结构:

堆栈跟踪:

代码语言:javascript
复制
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

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

EN

回答 2

Stack Overflow用户

发布于 2015-03-24 17:20:34

将您的文件位置定义为资源,似乎不是一个好名字,如果您使用maven将您的文件放在资源文件夹下,并将您的属性占位符写为

代码语言:javascript
复制
<context:property-placeholder location="classpath:/path.properties" />

如果你没有使用maven在你的配置中定义文件夹它是classes (我建议重命名它)在你的配置中,在这个选项卡上定义这个文件夹Build Path->Configure Build Path->Source Add classes文件夹

票数 1
EN

Stack Overflow用户

发布于 2015-03-24 21:03:42

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29228124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档