gpt4 book ai didi

java - 类路径资源 [classpath* :xxxxx. 属性] 无法打开,因为它不存在

转载 作者:行者123 更新时间:2023-11-30 07:52:40 25 4
gpt4 key购买 nike

我正在尝试在当前的 spring 应用程序中实现某种条件导入 bean 上下文文件。为了使这项工作正常进行,我在 SO 中使用类似的解决方案来扩展 ApplicationContextInitializer<ConfigurableApplicationContext>

package com.mydepartment.app.util;

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment env = applicationContext.getEnvironment();

// load from web.xml context-param
String propertyFileClassPath = env.getProperty("propertyFileClassPath");

try {
MutablePropertySources sources = env.getPropertySources();
sources.addFirst(new ResourcePropertySource(new ClassPathResource(propertyFileClassPath)));
} catch (IOException ioException) {
LOG.info(ioException.getMessage());
LOG.error( "Loading resource failed..", ioException);
}
}
}

这就是我希望添加到 web.xml 中以使当前应用程序能够加载包含定义条件的属性的属性文件

<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>com.mydepartment.app.util.MyApplicationContextInitializer</param-value>
</context-param>
<context-param>
<param-name>propertyFileClassPath</param-name>
<param-value>classpath*:application-env.properties</param-value>
</context-param>

.....
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

application-env.properties位于

%war_file_name%\WEB-INF\classes\application-env.properties

但上述配置/编码在 WebLogicWebsphere 中返回以下错误,这让我对修复感到非常痛苦:

2015-10-15 13:23:12,625 -- INFO -- com.mydepartment.app.util.MyApplicationContextInitializer -- class path resource [classpath*:application-env.properties] cannot be opened because it does not exist
2015-10-15 13:23:12,626 -- ERROR -- com.mydepartment.app.util.MyApplicationContextInitializer -- Loading resource failed..

java.io.FileNotFoundException:类路径资源 [classpath*:application-env.properties] 无法打开,因为它不存在

我尝试了几种模式来建立类路径,但都失败了,例如:

classpath*:application-env.properties
file:/WEB-INF/classes/application-env.properties
/WEB-INF/classes/application-env.properties
../classes/application-env.properties

我可能希望应用程序从应用程序内的属性文件加载初始化属性,而不是系统属性或服务器属性。但我认为没有必要将属性文件添加到服务器的类路径中。

我没有发现应用程序服务器阻止访问此内部属性文件的任何原因。

最佳答案

当我们使用ClassPathResource时,我们直接指定资源路径。请尝试以下操作:

ClassPathResource cr = new ClassPathResource("application-env.properties");

关于java - 类路径资源 [classpath* :xxxxx. 属性] 无法打开,因为它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155126/

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