springboot系列教程(二)——Banner、CommandLineRunner、Arguments、Exit
来源:互联网 发布:放逐之城 修改数据 编辑:程序博客网 时间:2024/06/14 04:50
Banner
生成工具地址 可以生成图片或者文字
http://patorjk.com/software/taag
http://www.network-science.de/ascii/
http://www.degraeve.com/img2txt.php
程序猿专用banner图
${AnsiColor.BRIGHT_YELLOW} //////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : |||// \ // // / _||||| -:- |||||- \ // // | | \\\ - /// | | // // | \_| ''\---/'' | | // // \ .-\__ `-` ___/-. / // // ___`. .' /--.--\ `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;`\ _ /`;.`/ - ` : | | // // \ \ `-. \_ __\ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 永不宕机 永无BUG // ////////////////////////////////////////////////////////////////////
设置banner开关
SpringApplication app = new SpringApplication(Study2Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args);
new SpringApplicationBuilder() .sources(Parent.class) .child(Study2Application.class) .bannerMode(Banner.Mode.OFF) .run(args);
CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。
为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。
新建java类MyInit1
@Component@Order(value = 1)public class MyInit1 implements CommandLineRunner { @Override public void run(String... strings) throws Exception { System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<"); }}
新建java类MyInit2
@Component@Order(value = 2)public class MyInit2 implements CommandLineRunner { @Override public void run(String... strings) throws Exception { System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<"); }}
运行结果
来看看springboot在启动的时候为我们注入了哪些bean,也可以这样:
package com.example.demo;import javafx.scene.Parent;import org.springframework.boot.Banner;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Bean;import java.util.Arrays;@SpringBootApplicationpublic class Study2Application { public static void main(String[] args) {/* SpringApplication app = new SpringApplication(Study2Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args);*/ new SpringApplicationBuilder() .sources(Parent.class) .child(Study2Application.class) .bannerMode(Banner.Mode.CONSOLE) .run(args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx){ return args -> { System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; }}
Application Arguments
如果需要访问SpringApplication.run(…)传入的参数,可以通过注解的形式注入
ApplicationArguments接口实例,该接口提供了一套访问String[]参数的方法和
【不知道怎么翻译,原文是】as well as parsed option and non-option arguments:
package com.example.demo.bean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.ApplicationArguments;import org.springframework.stereotype.Component;import java.util.List;/** * @author :小虎 * @date :2017/12/21 */@Componentpublic class MyBean { @Autowired public MyBean(ApplicationArguments args) { boolean debug = args.containsOption("debug"); List<String> files = args.getNonOptionArgs(); // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"] }}
Application Exit
新建java类ExitConfig
@Componentpublic class ExitConfig implements ExitCodeGenerator { @Override public int getExitCode() { System.out.println("定义程序退出的退出码"); return 1024; }}
新建退出controller
@RestControllerpublic class ExitController extends ApplicationObjectSupport{ @Autowired private ExitConfig exitConfig; @RequestMapping("/exit") public void exit(){ SpringApplication.exit(super.getApplicationContext(),exitConfig); }}
访问地址 http://localhost:8080/exit
代码地址
https://github.com/itmybaby/springboot/tree/master/study2
阅读全文
0 0
- springboot系列教程(二)——Banner、CommandLineRunner、Arguments、Exit
- springboot系列教程(五)——整合jpa
- springboot系列教程(六)——整合jdbctemplate
- springboot系列教程(七)——整合mybatis
- springboot系列教程(八)——整合thymeleaf、freemarker
- 织梦仿站系列教程第八讲——通用头部(四)LOGO与BANNER
- springboot系列教程(一)——第一个springboot程序
- SpringBoot之启动加载数据 CommandLineRunner(G)
- SpringBoot:spring boot CommandLineRunner启动
- springboot实战之整合CommandLineRunner
- springboot系列教程(三)——配置方式、多环境配置
- SpringBoot系列—概述
- springboot系列之一——springboot入门
- CoffeeScript系列教程(二)—安装和使用
- SpringBoot学习(二)——入门
- SpringBoot系列—基础配置
- SpringBoot系列—SSL配置
- SpringBoot系列—Redis使用
- Ubuntu16.04 适用docker安装MySQL,并将数据挂载到本地目录
- v4l2细节
- 关于C语言相关操作符的个人剖析
- Leetcode Note:1.Single Number问题
- C语言操作符
- springboot系列教程(二)——Banner、CommandLineRunner、Arguments、Exit
- linux中fork()函数详解
- Thymeleaf模板与Spring MVC集成
- Java魔法堂:注解用法详解——@SuppressWarnings
- YOLOv2源码分析(四)
- Face-landmarks-detection-benchmark 人脸特征定位网站汇总
- 【好书推荐】程序员的数学 pdf 1-2-3
- NOIP 2016 天天爱跑步
- chunk在nodejs中的体现