使用spring的监听器来完成系统超级管理员的注册

1.注入

2“util类

 1 package com.liveyc.mgrsite.util;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.context.ApplicationListener;
 5 import org.springframework.context.event.ContextRefreshedEvent;
 6 import org.springframework.stereotype.Component;
 7 
 8 import com.liveyc.eloan.base.service.ILoginInfoService;
 9 import com.liveyc.eloan.util.BidConst;
10 
11 /**
12  * 初始化超级管理员的监听器
13  * InitAdminListener
14  * @Description:
15  * 1:在spring中,实现了 ApplicationListener接口类就可以作为spring的监听器来监听spring中特殊的事件
16  * 2:在spring中,ApplicationEvent这个类相当于所有的事件,如果我们的监听器实现ApplicationListener<ApplicationEvent>
17  * 就相当于我们这个监听器监听的是spring容器中所有的消息
18  * 3:现在我们只想监听spring启动完成的事件,只要监听ContextRefreshedEvent事件就可以了
19  * @author:  xuyou
20  * @date:   2017年12月25日
21  */
22 @Component
23 public class InitAdminListener implements ApplicationListener<ContextRefreshedEvent> {
24     
25     @Autowired
26     private ILoginInfoService loginInfoService;
27 
28     @Override
29     public void onApplicationEvent(ContextRefreshedEvent event) {
30         //检查超级管理员  DEFAULT_ADMIN_NAME = "admin"
31         if(this.loginInfoService.checkUsername(BidConst.DEFAULT_ADMIN_NAME)){
32             //如果没有创建一个
33             loginInfoService.createDefaultAdmin();
34         }
35     }
36 
37 }

 数据库添加了admin账户

转载于:https://www.cnblogs.com/xuyou551/p/8110700.html

原文地址:https://www.cnblogs.com/twodog/p/12137839.html