java 加载顺序
构造方法 ——> @Autowired —— > @PostConstruct ——> 静态方法
CommandLineRunner
这个是服务起来之后,会优先加载
另外还可以
@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
InitializingBean, ApplicationListener<ContextRefreshedEvent> {
private Logger logger = LogManager.getLogger();
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
logger.info("1 => StartupListener.setApplicationContext");
}
@Override
public void setServletContext(ServletContext context) {
logger.info("2 => StartupListener.setServletContext");
}
@Override
public void afterPropertiesSet() throws Exception {
logger.info("3 => StartupListener.afterPropertiesSet");
}
@Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
logger.info("4.1 => MyApplicationListener.onApplicationEvent");
if (evt.getApplicationContext().getParent() == null) {
logger.info("4.2 => MyApplicationListener.onApplicationEvent");
}
}
}
//加载循序是按编号