SpringBean initialization sequence

Original link

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

@AutowiredTherefore, the defined parameters cannot be used directly in the constructor , but should be @PostConstructcalled 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:

code path

 
 

Guess you like

Origin blog.csdn.net/zbzcDZF/article/details/131212118