Spring beans are objects managed by the Spring framework at runtime. Spring beans are the basic building blocks of any Spring application. Most of the application logic code we write will be placed in Spring beans.
Execution order:
Constructor > @Autowired > @PostConstruct > InitializingBean > init-method
@Autowired
Therefore, the defined parameters cannot be used directly in the constructor , but should be @PostConstruct
called in the constructor.
For example:
public class Metric {
@Autowired
MonitorMetric monitorMetric;
final String string;
// 构造函数
public Metric(String string){
this.string = string;
}
// 在 PostConstruct 中获取 Autowired 的值
@PostConstruct
public String initialize(){
return monitorMetric.getStr();
}
}
To learn more programming knowledge, please follow my official account: