无法将@ConfigurationProperties bean映射到@Component bean

时间:2018-02-27 22:59:48

标签: java spring

配置类的实例已正确创建,但未正确映射到@Component类,显示为null。为什么要@Component类注释,因为@Autowired似乎根本不起作用?谢谢。

@Configuration    
@ConfigurationProperties(prefix="wmi2017")    
@PropertySource(value = "classpath:wmi2017.properties",  ignoreResourceNotFound = false)

public class Wmi2017Properties {
}

- >没有@Configuration,这不起作用

@Component
@EnableConfigurationProperties(Wmi2017Properties.class)
public class WmiCollection {

    @Autowired
    private Wmi2017Properties wmiProps;

@Autowired不起作用且wmiProsnull

1 个答案:

答案 0 :(得分:0)

试试这个:

@Component
@EnableConfigurationProperties(Wmi2017Properties.class)
public class WmiCollection {

    //@Autowired
    private Wmi2017Properties wmiProps;

public WmiCollection (Wmi2017Properties wmiProps){

this.wmiProps = wmiProps;
}