@postConstruct constructor afterSetProperties() setApplicationContext 执行顺序
Posted kaoli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@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 执行顺序的主要内容,如果未能解决你的问题,请参考以下文章
Mockito + Spring + @PostConstruct,mock初始化错误,为啥会调用@PostConstruct?