springboot系列教程(二)——Banner、CommandLineRunner、Arguments、Exit

生成工具地址 可以生成图片或者文字

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;

@SpringBootApplication
public 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
 */


@Component
public 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

@Component
public class ExitConfig implements ExitCodeGenerator {
    @Override
    public int getExitCode() {
        System.out.println("定义程序退出的退出码");
        return 1024;
    }
}

新建退出controller

@RestController
public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值