接口定义:接口,用于指示bean包含在SpringApplication中时应运行。可以在同一应用程序上下文中定义多个CommandLineRunner bean,并可以使用ordered接口或@Order注释进行排序。
用法:
@Component public class InitPermissionRoles implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(InitPermissionRoles.class); @Autowired private SysPermissionServiceImpl sysPermissionService; @Override public void run(String... args) throws Exception { LOGGER.info("开始初始化权限"); try { sysPermissionService.refreshPermissionRolesCache(); }catch (Exception e){ LOGGER.error("权限初始化错误,错误信息为{}",e.getMessage()); } } }
应用场景为:初始化项目加载数据场景