1,在测试禁用banner的时候启动springboot项目报了上面的错误,以下是启动类的代码:
@SpringBootApplication
public class Springbootday011Application {
public static void main(String[] args) {
//SpringApplication.run(Springbootday011Application.class, args);
SpringApplicationBuilder builder = new SpringApplicationBuilder();
builder.bannerMode(Banner.Mode.OFF).run(args);
}
}
原因:忘了给SpringApplicationBuilder传一个sources参数了(当前类的对象),以此参数来创建一个SpringApplication实例。上面代码应改为
@SpringBootApplication
public class Springbootday011Application {
public static void main(String[] args) {
//SpringApplication.run(Springbootday011Application.class, args);
SpringApplicationBuilder builder = new SpringApplicationBuilder(Springbootday011Application.class);
builder.bannerMode(Banner.Mode.OFF).run(args);
}
}
其实SpringApplication.run(Springbootday011Application.class, args);底层也是通过sources来创建SpringApplication实例的
下面分析一下builder.bannerMode(Banner.Mode.OFF).run(args);为什么可以禁用了banner打印?
SpringApplication会调用setBannerMode方法给bannerMode赋值,bannerMode默认是CONSOLE
当调用run()方法的时候 会执行printBanner()方法
而printBanner方法里面会判断bannerMode,如果是OFF就不打印