java - Spring 不加载属性

标签 java spring properties-file

在 @Configuration 类中引用 applicationContext.xml 时,Spring 不加载属性

@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:(在类路径中)

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

使用“myBean1”加载 test.properties 文件没有问题,从属性文件中获取 ${name} 和 {age} 的值。

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

ApplicationContext ctx =   new AnnotationConfigApplicationContext(SpringConfig.class);

SpringConfig 类引用 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 在我的类路径中,它与属性文件丢失或其他问题无关。

我不知道我是否必须对 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 在解析属性时是快速失败的。也就是说,如果它没有找到匹配项,它将抛出异常(实际上执行解析的嵌套类会这样做)。

由于您的 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。

关于java - Spring 不加载属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30549350/

相关文章:

javascript - 使用自己的消息 CQ 登录

docker 中 rabbitmq 的 Spring Boot 应用程序问题

Java:无法加载属性文件。为什么?

java - 让 IntelliJ 知道属性文件

java - 在获取数组中的重复数字时出现错误

java - 从java中的不同类访问变量

java - 在 Spring Boot 应用程序中共享类的实例

spring - 正确使用 Spring 环境配置文件以管理 PropertySourcesPlaceholderConfigurer 和属性文件集

java - 如何为新的 WiFi 配置设置静态 IP?

java - 带有一个特定参数的 Spring AOP 切入点