@postConstruct constructor afterSetProperties() setApplicationContext 执行顺序

测试代码:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
@Component
public class LoadSequence implements InitializingBean, ApplicationContextAware {
@Value("${DISTRIBUTED_LOCK}")
private String code;


public LoadSequence(){
System.out.println("constructor has runned:" + code);
}
@PostConstruct
public void postConstruct(){
System.out.println("postConstruct has runned:" + code);
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet has runned:" + code);
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("setApplicationContext has runned:" + code);
}
}


结果:

constructor has runned:null
setApplicationContext has runned:false
postConstruct has runned:false
afterPropertiesSet has runned:false

@postConstruct constructor afterSetProperties() setApplicationContext 执行顺序

上一篇:create-react-app 构建的项目使用代理 proxy


下一篇:用ModelAndView返回视图结果返回的是对应RequestMapping拼接的路径