简体   繁体   English

删除PropertySourcesPlaceHolderConfigurer bean自动装配不起作用

[英]Removing PropertySourcesPlaceHolderConfigurer bean Autowiring doesn't work

What is the purpose of adding "PropertySourcesPlaceholderConfigurer" as a bean in spring? 在春季将“ PropertySourcesPlaceholderConfigurer”添加为bean的目的是什么? . As far as I researched it says when you name your property file as application.properties, spring will automatically take the file when it is in src/main/resources Folder. 据我研究,当您将属性文件命名为application.properties时,spring会在src / main / resources文件夹中自动获取该文件。 Having so, when I remove the Bean Declaration for PropertySourcesPlaceholderConfigurer, it says couldn't autowire the property. 这样,当我删除PropertySourcesPlaceholderConfigurer的Bean声明时,它说无法自动装配该属性。

What am I missing, Why isn't it working without PropertySourcesPlaceholderConfigurer 我想念的是什么,如果没有PropertySourcesPlaceholderConfigurer怎么办?

Sample 1: 范例1:

@Configuration
@PropertySource(value = { "application.properties" }, ignoreResourceNotFound = true)
@ComponentScan(basePackages = { Some Package })
public class ApplicationConfig {

    ApplicationConfig() {

    }

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

}

Sample 1 works fine, When I remove @PropertySources and @PropertySourcesPlaceholderConfigurer bean since spring will pick up application.properties automatically, it doesn't work. 示例1运行良好,当我删除@PropertySources@PropertySourcesPlaceholderConfigurer bean时,因为spring将自动拾取application.properties ,所以它不起作用。

it says when you name your property file as application.properties, spring will automatically take the file when it is in src/main/resources Folder 它说,当您将属性文件命名为application.properties时,spring会在src / main / resources文件夹中自动获取该文件

As I know - this is correct only for Spring Boot. 据我所知-这仅对Spring Boot是正确的。 For spring core you should define you prps in contxet. 对于spring core,应该在contxet中定义prps。 There are several ways. 有几种方法。 In xml 在xml中

< context:property-placeholder location="classpath:application.properties" />

or using Java annotation 或使用Java注释

@Configuration
@PropertySource("classpath:application.properties")

It's not necessary to define PropertySourcesPlaceholderConfigurer since Spring 4.3RC2 从Spring 4.3RC2开始,不必定义PropertySourcesPlaceholderConfigurer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM