在@PropertySource中解析占位符

时间:2015-06-10 18:31:56

标签: java spring

我尝试创建一个配置类,使用@PropertySource注释从文件加载应用程序属性,但PropertySourcesPlaceholderConfigurer似乎没有设置从注释中接收的位置。

@Configuration
@ComponentScan("com.just.a.test")
@PropertySources({
    @PropertySource(value="file:C:\\tmp\\1.cfg" 
            , ignoreResourceNotFound=true)
})
public class TestConfig {
    @Value("${just.a.string}")
    String justAString;


    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
       return propertySourcesPlaceholderConfigurer;
    }
}

应用程序属性文件是:

just.a.string=DOH

我收到以下异常:

  

....       引起:java.lang.IllegalArgumentException:无法解析占位符&just.a.string'在字符串值" $ {just.a.string}"
          在org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
          在org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
          在org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
          在org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
          在org.springframework.context.support.PropertySourcesPlaceholderConfigurer $ 2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
          在org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
          在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955)
          在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
          在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
          ......还有15个

但是,如果我使用PropertySourcesPlaceholderConfigurer bean设置位置,一切正常。

@Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocation(new FileSystemResource("c:\\tmp\\1.cfg"));
       return propertySourcesPlaceholderConfigurer;
    }

我正在使用Spring 4.1.6-RELEASE和jdk 8.05。

提前感谢。

更新1

如sivaprasadreddy.k所述。上面的代码确实工作正常,我忘了在我的原始代码中我使用 -

从JVM参数中检索文件
@PropertySource(value="file:#{systemProperties['config.path']}" 

并传递了一个jvm参数:

-Dconfig.path=C:\\tmp\\1.cfg

@Sotirios - 我只是使用像这样的AnnotationConfigApplicationContext(Class) -

public static void main(String[] args) { 
new AnnotationConfigApplicationContext(TestConfig.class);
 }

更新2

基本上我想要实现的是:

1)使用jvm param加载属性文件,其名称定义为const

a)如果找到该文件,则继续

b)否则,如果存在定义为const的默认文件路径,则继续

c)否则继续。

2)尝试使用其名称定义为const

的属性来解析@Value

a)如果发现继续

b)否则使用const。

加载默认值 抱歉这些空间。编辑出了点问题。

2 个答案:

答案 0 :(得分:1)

考虑您的编辑

@PropertySource(value="file:#{systemProperties['config.path']}" 

你必须首先意识到@Configuration类(及其注释)在其中声明的@Bean之前被解析和处理。换句话说,在Spring处理PropertySourcesPlaceholderConfig注释时,您的#{..}(确定@PropertySource占位符的解析)尚未注册。它不可用。

相反,Spring可以执行PropertySourcesPlaceholderConfig提供的行为的子集,只有${..}占位符。 (See the javadoc。)并且它只能使用已在ApplicationContext的{​​{1}}注册的媒体资源。在您的示例中,(应该)只有两个来源,即系统属性和系统环境。

符号

Environment

陈述:在名为systemProperties['config.path'] 的属性来源中提供属性config.path的值。我不确定Spring检查上面命名的属性源的顺序,但是如果你知道只有你的系统属性(而不是环境)有systemProperties属性,你可以简单地使用

config.path

答案 1 :(得分:-1)

尝试:

@Configuration
@ComponentScan("com.just.a.test")
@PropertySource(value = {"file:C:/tmp/1.cfg/application.properties",
                    "classpath:config.properties" //if same key, this will 'win'
                                  }, ignoreResourceNotFound = true)
public class TestConfig {
   @Value("${just.a.string}")
   String justAString;


@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
                        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
                       return propertySourcesPlaceholderConfigurer;
                    }
                }
相关问题