Spring 服务启动 自动执行(ApplicationListener)

来源:互联网 发布:未来软件小说 编辑:程序博客网 时间:2024/06/06 02:27
[java] view plain copy
 print?
  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.context.ApplicationListener;  
  3. import org.springframework.context.event.ContextRefreshedEvent;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. /** 
  7.  *  
  8.  * @author yuki_ho 
  9.  * 
  10.  */  
  11. @Service  
  12. public class StartAddDataListener implements ApplicationListener<ContextRefreshedEvent>  
  13. {  
  14.   
  15.   
  16.     @Override  
  17.     public void onApplicationEvent(ContextRefreshedEvent event)  
  18.     {  
  19.         if(event.getApplicationContext().getParent() == null)//root application context 没有parent,他就是老大.  
  20.         {   
  21.             //需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。   
  22.       
  23.         }   
  24.          
  25.   
  26.          
  27.     }  
  28.   
  29. }  
原创粉丝点击