Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不同的上下文中使用多个属性占位符_Java_Spring_Spring Security_Properties File - Fatal编程技术网

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

Java 如何在不同的上下文中使用多个属性占位符,java,spring,spring-security,properties-file,Java,Spring,Spring Security,Properties File,我使用Spring框架和Spring安全性。我需要在不同的地方使用页面路径。例如,“/mainpath”。 我将它放在/WEB-INF/classes/path.properties中。 路径应在servlet-context.xml和security-context.xml中使用。 如果在两种上下文中都放置了以下内容 <context:property-placeholder location="/WEB-INF/classes/path.properties"/&g

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

<context:property-placeholder location="/WEB-INF/classes/path.properties"/>
以下内容未给出所需的结果

<context:property-placeholder location="/WEB-INF/classes/path.properties" ignore-unresolvable="true"/>
我从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>
UPD2。 问题出在datasource-tx-jpa.xml中的属性占位符中,该占位符在security-context.xml之前导入到root-context.xml中。因此,在与datasource-tx-jpa.xml关联的属性文件中找不到占位符。
属性占位符的属性“ignore unsolvable”应在第一个导入的上下文中设置为true。

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

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


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

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

问题的主题和内容之间存在严重差异。您确定属性文件包含main.page作为密钥吗?你能把它作为你问题的一部分,贴在准确的stacktrace旁边吗?@KurtDuBois你说得对。主题和内容不一样,所以我重新命名了这个问题。我也找到了决策。别忘了你不应该尝试加载两个不同的上下文:属性占位符标记。即使在不同的地方subcontexts@MarianP在这种情况下,如何在不同的子上下文中使用属性?您只能加载它们一次,因此它需要位于一个位置
<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>
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
<context:property-placeholder location="classpath:/path.properties" />