spring boot项目01:非Web项目(基础)

Eclipse版本:2021-03 (4.19.0)

Java:8

Spring Boot:spring-boot-starter-parent#2.5.2(对于的spring-core/context/beans的版本为 5.3.8)

来自 博客园

nothing 项目介绍:

本地Java项目(Maven项目)——不提供各种Web服务。程序可以在 main函数、CommandLineRunner、ApplicationRunner 中运行,还介绍了日志线程 的使用。

操作:

进入 https://start.spring.io/ 网站;

左边的选择好后,右边不选择 Dependencies;

下载后得到:来自 博客园

导入Eclipse后得到:

运行程序,输出下面内容后,程序退出:

修改 NothingApplication#main:

@SpringBootApplication
public class NothingApplication {

	public static void main(String[] args) {
		System.out.println("进入main...");
		
		SpringApplication.run(NothingApplication.class, args);
		
		System.out.println("启动项目...");
        }
}

输出:

注意,两个 println 的位置。来自 博客园

SpringApplication.run(...)做了什么?需要阅读源码才是,TODO

怎么让项目不退出呢?使用循环。

        System.out.println("启动项目...");
        
        int sleepTime = 0;
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(++sleepTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

运行,点击Eclipse的停止按钮,等待15秒左右,程序停止:

点击后,立即输出下面的日志:

2021-07-12 12:56:32.011  INFO 14088 --- [on(2)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.

疑问:

为何要等待15秒呢?期间做了什么?TODO

不过,连续点击两次 停止按钮,项目会立即结束。来自 博客园

nothing项目 其实也有依赖包:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

依赖 spring-boot-starter 包,这个包又依赖了 其它spring基础包(spring-core, spring-context, spring-boot等),所以项目才能运行起来。

CommandLineRunner 和 ApplicationRunner 接口

在spring boot参考手册中可以看到:

 

这两个接口功能相同,但其 run函数的参数不同,实现这两个函数可以执行一些 特定的代码。来自 博客园

需要使用注解 @Component ,默认情况下,实现 ApplicationRunner 的类 会比 CommandLineRunner 的提前执行,但可以使用 @Order 注解设置执行顺序。

代码:

@Order(1)
@Component
class Clrunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("running in Clrunner...");
        System.out.println(args.length);
        for (int i=0; i<args.length; i++) {
            System.out.println(i + "," + args[i]);
        }
    }
    
}

@Order(2)
@Component
class Apprunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("running in Apprunner...");
        
        String[] params = args.getSourceArgs();
        System.out.println(params.length);
        for (int i=0; i<params.length; i++) {
            System.out.println(i + "," + params[i]);
        }
        
    }
    
}

启动时配置了参数 123 abc,执行效果如下:按照 @Order 配置的顺序执行,一个接一个执行。来自 博客园

日志:

启动的时候,可以看到项目在 控制台 输出了日志了。因为spring boot项目已经依赖了日志相关的包,开发人员直接使用即可。

为了符合项目需求,开发人员还需要做配置,比如,修改 logging-spring.xml文件。

Logger 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SpringBootApplication
public class NothingApplication {

    public static Logger logger = LoggerFactory.getLogger(NothingApplication.class);
    
    public static void main(String[] args) {
//        System.out.println("进入main...");
        logger.info("进入main...");
        
        SpringApplication.run(NothingApplication.class, args);
        
//        System.out.println("启动项目...");
        logger.warn("启动项目...");

    }

}

输出结果:

输出了两句日志,但是,第一句日志的格式 和 之后的不一致,原因是什么呢?以及,怎么配置这个格式?TODO

看看nothing项目的日志依赖:

spring boot的文档中可以找到更多信息:

至于怎么配置日志格式?spring boot文档中有说明,也可以看 参考资料中的相关博文(#3)。来自 博客园

线程

可以在spring boot程序中 启动线程执行任务,线程不退出,则程序不退出。

@SpringBootApplication
public class NothingApplication {

    public static Logger logger = LoggerFactory.getLogger(NothingApplication.class);
    
    public static void main(String[] args) {
        logger.info("进入main...");
        
        ConfigurableApplicationContext caContext = SpringApplication.run(NothingApplication.class, args);
        
        logger.warn("启动项目...");
        
        // 启动线程
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                while(true) {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                        System.out.println("main sleep 2: [" + Thread.currentThread().getName() + "]");
                    } catch (InterruptedException e) {
                        System.out.println(e);
                    }
                }
            }
        }).start();
        
    }

}

输出:

没有启动线程时,main线程中输出 【启动项目...】的日志就结束了。有了上面的线程后,线程中的循环会一直执行,程序也不会退出了。

参考资料:

1、SpringBoot使用CommandLineRunner和ApplicationRunner执行初始化业务

2、SpringBoot与日志配置

3、Spring Boot logback 日志配置

4、

原文地址:https://www.cnblogs.com/luo630/p/15001636.html