gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:19:04 25 4
gpt4 key购买 nike

我使用 Spring 框架和 Spring Security。我需要在不同的地方使用页面路径。例如,“/主路径”。我将其放置在/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 中排除了 property-placeholder。错误仍然出现。security-context部分:

<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

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

最佳答案

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

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

如果您不使用maven在配置中定义文件夹,它是配置中的类(我建议重命名它),请定义此文件夹构建路径->配置构建路径->源添加类此选项卡上的文件夹

关于java - 如何在不同的上下文中使用多个属性占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29228124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com