首页 > [坐等]Spring非Compnent如何注入配置项的值

[坐等]Spring非Compnent如何注入配置项的值

我们知道spring中可以如下这样注入配置项的值,

@Compnent
public class Foo {
    @Value("${bar}")
    private String foo;    
}

但我现在的前提是Foo为一个非SpringBean(即非Compnent),那如何实现?


既然不是spring的bean,不受spring管理,自然也拿不到spring的配置项了。能想到这两种办法:

  1. 自己去解析配置文件,读配置项

  2. 调用foo的类是有@Component的,在创建foo对象时通过foo的构造函数传给它配置

【热门文章】
【热门文章】