我在web应用程序中使用Spring3.2,我希望在类路径中有一个包含默认值的.properties文件。用户应该能够使用JNDI来定义存储另一个.properties的位置,该位置将覆盖缺省值。
只要用户设置了configLocation as JNDI属性,下面的操作就有效。
@Configuration
@PropertySource({ "classpath:default.properties", "file:${java:comp/env/configLocation}/override.properties" })
public class AppConfig
{
}但是,外部覆盖应该是可选的,JNDI属性也应该是可选的。
目前我得到了一个异常(java.io.FileNotFoundException: comp\env\configLocation\app.properties (The system cannot find the path specified)当JNDI属性丢失时。
如何定义仅在设置了JNDI属性(configLocation)时才使用的可选.properties?使用@PropertySource可以做到这一点吗?或者有其他解决方案吗?
发布于 2013-10-11 19:28:30
尝试以下操作。创建ApplicationContextInitializer
在Web上下文中:ApplicationContextInitializer<ConfigurableWebApplicationContext>并通过以下方式在web.xml中注册它:
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>...ContextInitializer</param-value>
</context-param>在ContextInitializer中,您可以通过类路径和文件系统添加属性文件(虽然还没有尝试过)。
public void initialize(ConfigurableWebApplicationContext applicationContext) {
String activeProfileName = null;
String location = null;
try {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String appconfigDir = environment.getProperty(APPCONFIG);
if (appconfigDir == null ) {
logger.error("missing property: " + APPCONFIG);
appconfigDir = "/tmp";
}
String[] activeProfiles = environment.getActiveProfiles();
for ( int i = 0; i < activeProfiles.length; i++ ) {
activeProfileName = activeProfiles[i];
MutablePropertySources propertySources = environment.getPropertySources();
location = "file://" + appconfigDir + activeProfileName + ".properties";
addPropertySource(applicationContext, activeProfileName,
location, propertySources);
location = "classpath:/" + activeProfileName + ".properties";
addPropertySource(applicationContext, activeProfileName,
location, propertySources);
}
logger.debug("environment: '{}'", environment.getProperty("env"));
} catch (IOException e) {
logger.info("could not find properties file for active Spring profile '{}' (tried '{}')", activeProfileName, location);
e.printStackTrace();
}
}
private void addPropertySource(ConfigurableWebApplicationContext applicationContext, String activeProfileName,
String location, MutablePropertySources propertySources) throws IOException {
Resource resource = applicationContext.getResource(location);
if ( resource.exists() ) {
ResourcePropertySource propertySource = new ResourcePropertySource(location);
propertySources.addLast(propertySource);
} else {
logger.info("could not find properties file for active Spring profile '{}' (tried '{}')", activeProfileName, location);
}
}上面的代码尝试查找每个活动配置文件的属性文件(请参阅:How to set active spring 3.1 environment profile via a properites file and not via an env variable or system property)
发布于 2014-01-10 21:24:42
从Spring4开始,问题SPR-8371已经解决了。因此,@PropertySource注释具有一个名为ignoreResourceNotFound的新属性,该属性正是为此目的而添加的。此外,还有新的@PropertySources注释,它允许如下实现:
@PropertySources({
@PropertySource("classpath:default.properties"),
@PropertySource(value = "file:/path_to_file/optional_override.properties", ignoreResourceNotFound = true)
})发布于 2014-05-27 19:02:36
如果您还没有使用Spring4(请参阅matsev的解决方案),这里有一个更冗长但大致相同的解决方案:
@Configuration
@PropertySource("classpath:default.properties")
public class AppConfig {
@Autowired
public void addOptionalProperties(StandardEnvironment environment) {
try {
String localPropertiesPath = environment.resolvePlaceholders("file:${java:comp/env/configLocation}/override.properties");
ResourcePropertySource localPropertySource = new ResourcePropertySource(localPropertiesPath);
environment.getPropertySources().addLast(localPropertySource);
} catch (IOException ignored) {}
}
}https://stackoverflow.com/questions/17401583
复制相似问题