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

来源:互联网 发布:张万桑作品集知乎 编辑:程序博客网 时间:2024/05/21 22:38
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.stereotype.Service;/** *  * @author yuki_ho * */@Servicepublic class StartAddDataListener implements ApplicationListener<ContextRefreshedEvent>{    @Override    public void onApplicationEvent(ContextRefreshedEvent event)    {        if(event.getApplicationContext().getParent() == null)//root application context 没有parent,他就是老大.        {             //需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。         }                   }}

0 0