Spring Java based configuration with static method

后端 未结 1 1917
长发绾君心
长发绾君心 2021-02-02 16:11

can any one please advice why we need to declare PropertySourcesPlaceholderConfigurer bean using a static method ? I just found that if I use non-static for bel

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 16:42

    PropertySourcesPlaceholderConfigurer objects are responsible for resolving @Value annotations against the current Spring Environment and its set of PropertySources. PropertySourcesPlaceholderConfigurer class implements BeanFactoryPostProcessor. In the container lifecycle, a BeanFactoryPostProcessor object must be instantiated earlier than an object of @Configuration-annotated class.

    If you have @Configuration-annotated class with instance method returning a PropertySourcesPlaceholderConfigurer object, then the container can not instantiate the PropertySourcesPlaceholderConfigurer object without instantiating the @Configuration-annotated class object itself. In this case, @Value can not be resolved, since the PropertySourcesPlaceholderConfigurer object does not exist at the moment of instantiation of the object of @Configuration-annotated class. Thus, @Value-annotated field takes the default value, which is null.

    Please see the "Bootstrapping" part of @Bean javadoc for more information.

    0 讨论(0)
提交回复
热议问题