Java Springboot@ConfigurationProperties未注入

Java Springboot@ConfigurationProperties未注入,java,spring-boot,properties,configuration,Java,Spring Boot,Properties,Configuration,我无法将属性文件(export fields.yml)注入到映射中。我在resources文件夹中创建了属性文件,链接到属性源,创建了属性对象的配置,并使用lombok生成了getter和setter,但仍然没有将任何内容填充到字段映射中(fields=null)。有什么我遗漏的吗?这是我到目前为止的代码 FieldReplacerProperties.java @Getter @Setter @Component @ConfigurationProperties(prefix = "

我无法将属性文件(export fields.yml)注入到映射中。我在resources文件夹中创建了属性文件,链接到属性源,创建了属性对象的配置,并使用lombok生成了getter和setter,但仍然没有将任何内容填充到字段映射中(fields=null)。有什么我遗漏的吗?这是我到目前为止的代码

FieldReplacerProperties.java

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "export")
@PropertySource("classpath:export-fields.yml")
public class FieldReplacerProperties {
    private Map<String, String> fields;

public List<String> columnsToFields(List<String> columns){
        columns.parallelStream().forEach(column -> fields.get(column));
        return columns;
    }
我如何访问它

@Autowired
private FieldReplacerConfiguration fieldReplacerConfiguration;
//replace columns with fields
fieldReplacerConfiguration.columnsToFields(columns);

弹簧从application.properties(或yml)加载属性。尝试将属性从export-fields.yml移动到application.yml

弹簧从application.properties(或yml)加载属性。尝试将属性从export-fields.yml移动到application.yml

添加了一个PropertySourceFactory,这解决了这个问题

@属性源更改

@PropertySource(value = "classpath:export-fields.yml", factory = YamlPropertySourceFactory.class)
YML工厂

public class YamlPropertySourceFactory implements PropertySourceFactory {

@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(encodedResource.getResource());

Properties properties = factory.getObject();

return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);

}

}
公共类YamlPropertySourceFactory实现PropertySourceFactory{
@凌驾
公共属性源createPropertySource(字符串名称,EncodedResource EncodedResource){
YamlPropertiesFactoryBean工厂=新的YamlPropertiesFactoryBean();
setResources(encodedResource.getResource());
Properties=factory.getObject();
返回新属性PropertySource(encodedResource.getResource().getFilename(),属性);
}
}

添加了PropertySourceFactory,解决了此问题

@属性源更改

@PropertySource(value = "classpath:export-fields.yml", factory = YamlPropertySourceFactory.class)
YML工厂

public class YamlPropertySourceFactory implements PropertySourceFactory {

@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(encodedResource.getResource());

Properties properties = factory.getObject();

return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);

}

}
公共类YamlPropertySourceFactory实现PropertySourceFactory{
@凌驾
公共属性源createPropertySource(字符串名称,EncodedResource EncodedResource){
YamlPropertiesFactoryBean工厂=新的YamlPropertiesFactoryBean();
setResources(encodedResource.getResource());
Properties=factory.getObject();
返回新属性PropertySource(encodedResource.getResource().getFilename(),属性);
}
}

这是否回答了您的问题@是的,谢谢你。我需要一个用于yml文件的PropertySourceFactory。这是否回答了您的问题@是的,谢谢你。我需要一个用于yml文件的PropertySourceFactory。在application.yml中有很多事情要做,所以我想把它移到外面,以便更容易维护。为了解决PropertySourceFactoryUnderstand的问题,避免实现PropertySourceFactory并加载自定义yml的另一个选择是遵循类似application-.yml的约定。示例application-fields-yml.Have在application.yml中进行了大量操作,因此希望将其移到外部,以便更容易维护。为了解决PropertySourceFactoryUnderstand的问题,避免实现PropertySourceFactory并加载自定义yml的另一个选择是遵循类似application-.yml的约定。示例应用程序字段yml。