【问题标题】:How to use several property-placeholder in different contexts如何在不同的上下文中使用多个属性占位符
【发布时间】:2015-03-24 08:41:19
【问题描述】:

我使用 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。 路径属性:

main.page=mainpage

我从 servlet-context.xml 中排除了属性占位符。错误仍然出现。 安全上下文部分:

<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 与其他属性文件。

文件夹结构:

堆栈跟踪:

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 中的属性占位符中,该占位符是在 security-context.xml 之前的 root-context.xml 中导入的。因此它无法在与 datasource-tx-jpa.xml 关联的属性文件中找到占位符。 在第一个导入的上下文中,property-placeholder 的“ignore-unresolvable”属性应该设置为 true。

【问题讨论】:

  • 您的问题主题与内容存在严重差异。您确定您的属性文件包含 main.page 作为键吗?您能否将其作为您问题的一部分发布在确切的堆栈跟踪旁边?
  • @KurtDuBois 你是对的。题目和内容不一样,所以我把问题重命名了。我也找到了决定。
  • 不要忘记你不应该尝试加载 2 个不同的 context:property-placeholder 标签。即使在不同的子上下文中
  • @MarianP 那么如何在不同的子上下文中使用属性?
  • 只能加载一次,所以需要放在一个地方

标签: java spring spring-security properties-file


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2013-01-05
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多