Spring 未加载属性

Spring not loading properties

Spring 在 @Configuration class

中引用 applicationContext.xml 时不加载属性
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class SpringConfig {

  @Autowired
    private MyBean1 myBean1;

  @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();

    }

  @Bean(name = "myBean2")
    public MyBean2 myBean2() {
    MyBean2 bean2 = new MyBean2();
    return bean2;
    }


}

applicationContext.xml:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location">           
                <value>classpath:test.properties</value>           
        </property>
    </bean>

  <bean id="myBean1" class="com.foo.bar.MyBean1">
        <property name="name" value="${name}" />  
        <property name="age" value="${age}" />  
    </bean>

Test.properties:(在class路径中)

name=foo
age=10

当我使用以下代码行加载 applicationContext.xml 文件时,它起作用了:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

我看到打印了以下几行:

2015-05-30 10:01:08.277 [INFO] org.springframework.beans.factory.config.PropertyPlaceholderConfigurer:172 - Loading properties file from class path resource [test.properties]
2015-05-30 10:01:08.292 [INFO] org.springframework.beans.factory.support.DefaultListableBeanFactory:596 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5c001eb0: defining beans [placeholderConfig,myBean1]; root of factory hierarchy

我在加载 test.properties 文件时没有问题,"myBean1" 从属性文件中获取 ${name} 和 {age} 的值。

我遇到的问题是当我尝试加载 SpringConfig:

ApplicationContext ctx =   new AnnotationConfigApplicationContext(SpringConfig.class);

SpringConfig class 引用 applicationContext.xml 为:@ImportResource("classpath:applicationContext.xml")

我看到的错误是:

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myBean1' defined in class path resource [applicationContext.xml]: Could not resolve placeholder 'name' in string value "${name}"
                            at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
                            at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174)
                            at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
                            at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
                            at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:669)
                            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
                            at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)

简而言之,当我使用 ClassPathXmlApplicationContext 加载 applicationContext 时,我没有问题:

工作:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

但是当它被 SpringConfig 引用时,我发现 applicationContext 没有看到我的属性文件。

不工作:

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

请注意 test.properties 在我的 class 路径中,这与属性文件丢失或其他问题无关。

我不知道是否必须对 AnnotationConfigApplicationContext 使用不同的方法,因为我需要将 myBean1 自动连接到 SpringConfig 并且 myBean1 使用 test.properties 文件中的一些属性。

使用时

new AnnotationConfigApplicationContext(SpringConfig.class);

您正在注册两个用于解析属性的 PlaceholderConfigurerSupport bean。一通Java

@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();

}

和一个通过导入 XML

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">           
            <value>classpath:test.properties</value>           
    </property>
</bean>

PropertySourcesPlaceholderConfigurer bean 在解析属性时是快速失败的。也就是说,如果找不到匹配项,它将抛出异常(实际上执行解析的嵌套 class 会执行此操作)。

因为你的 PropertySourcesPlaceholderConfigurer 还没有用任何 locations 初始化,它没有任何来源可以解析 ${name}${age}(它有一些来源,但不是您的 test.properties 文件)。

要么正确设置您的 bean 以找到您的属性文件

@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("test.properties"));
    return propertySourcesPlaceholderConfigurer;
}

或完全删除它。如果您删除它,Spring 将使用来自 XML.

的正确配置的 PropertyPlaceholderConfigurer bean