Spring ApplicationListener操作

想在spring容器启动时进行一些额外的初始化操作,那么就需要实现ApplicationListener接口,并重写onApplicationEvent方法:

@Component
public class SystemInit implements ApplicationListener<ContextRefreshedEvent> {
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		try {
			 if(event.getApplicationContext().getParent()==null)
			 {
				 // do something
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

相关推荐