class 路径资源 [classpath*:xxxxx.properties] 无法打开,因为它不存在
class path resource [classpath*:xxxxx.properties] cannot be opened because it does not exist
我正在尝试在当前 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
但在configuration/codingreturns以上WebLogic和Websphere都出现错误,这让我很痛苦修复:
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: class路径资源[classpath*:application-env.properties]无法打开,因为它不存在
我尝试了几种模式来建立class路径但都失败了,例如:
classpath*:application-env.properties
file:/WEB-INF/classes/application-env.properties
/WEB-INF/classes/application-env.properties
../classes/application-env.properties
可能我希望应用程序从应用程序内部的属性文件加载初始化属性,而不是系统属性或服务器属性。但我看不出有必要将属性文件添加到服务器的 class 路径中。
我看不出应用程序服务器阻止访问此内部属性文件的任何原因。
我们在使用ClassPathResource时,直接指定资源路径。请尝试以下操作:
ClassPathResource cr = new ClassPathResource("application-env.properties");
我正在尝试在当前 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
但在configuration/codingreturns以上WebLogic和Websphere都出现错误,这让我很痛苦修复:
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: class路径资源[classpath*:application-env.properties]无法打开,因为它不存在
我尝试了几种模式来建立class路径但都失败了,例如:
classpath*:application-env.properties
file:/WEB-INF/classes/application-env.properties
/WEB-INF/classes/application-env.properties
../classes/application-env.properties
可能我希望应用程序从应用程序内部的属性文件加载初始化属性,而不是系统属性或服务器属性。但我看不出有必要将属性文件添加到服务器的 class 路径中。
我看不出应用程序服务器阻止访问此内部属性文件的任何原因。
我们在使用ClassPathResource时,直接指定资源路径。请尝试以下操作:
ClassPathResource cr = new ClassPathResource("application-env.properties");