SpringBoot基础系列-基础


原创文章,转载请标注出处:《SpringBoot基础系列-基础》


一、创建可执行jar包

1.1 使用Maven插件:spring-boot-maven-plugin

在pom.xml中添加如下maven插件,创建SpringBoot项目会自动添加的

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1.2 运行maven命令进行打包

mvn package

1.3 运行jar包

打包成功后,你可以在项目的target目录中找到打包的结果,一个fat jar。

执行以下命令运行这个jar

java -jar target/xxx-0.0.1-SNAPSHOT.jar

二、自定义Banner

2.1 classpath路径下添加banner.txt文件

banner.txt文件中有要展示的内容,我们可以使用占位符变量:

序号 变量 说明
1 ${application.version} 应用版本
2 ${applicaiton.formatted-version} 格式化应用版本
3 ${spring-boot.version} springboot版本
4 ${spring-boot.formatted-version} 格式化springboot版本
5 {AnsiColor.NAME}or{AnsiStyle.NAME} ANSI转码名称
6 ${application.title} 应用名称

2.2 自定义banner.txt文件,使用如下属性指向该文件

spring.banner.location=/xxx/banner.txt
#如果文件不是UTF-8编码,需要这里手动指定编码类型
spring.banner.charset=

2.3 classpath路径下添加banner.gif、banner.jpg、banner.png文件

2.4 自定义banner.gif、banner.jpg、banner.png文件,使用如下属性指向该文件

spring.banner.image.location=/xxx/banner.gif

2.5 编程方式

@SpringBootApplication
public class SpringbootdemoApplication {
    public static void main(String[] args) {
        Banner banner = new Banner() {
            @Override
            public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
                out.println("就是要大于你");
            }
        };
        SpringApplication application = new SpringApplication(SpringbootdemoApplication.class);
        application.setBanner(banner);
        application.run(args);
    }
}

说明:

定制Banner的基础是我们要有一个实现了Banner类的实例,这里采用内部类的方式定义一个Banner实例

Banner banner = new Banner() {
    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        out.println("就是要大于你");
    }
};
application.setBanner(banner);// 设置Banner

定制Banner输出模式

applicaiton.setBannerMode(Banner.Mode.OFF)

Banner输出模式有三种:

enum Mode {
    // Disable printing of the banner.
    OFF,
    // Print the banner to System.out.
    CONSOLE,
    // Print the banner to the log file.
    LOG
}

三、定制SpringApplicaiton

我们在SpringBoot应用启动的main方法中创建一个SpringApplicaiton实例,然后就可以对其进行各种定制:

SpringApplication application = new SpringApplication(SpringbootdemoApplication.class);

另外,还可以使用Spring提供的SpringApplicaitonBuinder来进行流式定制。

SpringApplication application = new SpringApplicationBuilder()
    .bannerMode(Banner.Mode.CONSOLE).build();

定制实例:

@SpringBootApplication
public class SpringbootdemoApplication {
    public static void main(String[] args) {
        Banner banner = new Banner() {
            @Override
            public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
                out.println("就是要大于你");
            }
        };
        SpringApplication application = new SpringApplication(SpringbootdemoApplication.class);
        application.setBanner(banner);// 设置Banner
        application.setAddCommandLineProperties(false);//设置是否使用命令行属性
        application.setAddConversionService(false);// 
        application.setAdditionalProfiles("development");// 添加额外的profile
        application.setAllowBeanDefinitionOverriding(true);// 设置Bean定义可重写
        application.setApplicationContextClass(XmlWebApplicationContext.class);//设置使用的ApplicaitonContext实例类型
        application.setBannerMode(Banner.Mode.CONSOLE);//设置banner输出模式
        application.setMainApplicationClass(SpringbootdemoApplication.class);//设置main方法所在类
        application.setWebApplicationType(WebApplicationType.SERVLET);//设置应用类型
        application.setHeadless(true);// 
        application.setBeanNameGenerator();//设置自定义的bean name生成器
        application.setDefaultProperties();//设置默认的属性
        application.setEnvironment();//设置自定义的environment
        application.setInitializers();//设置自定义的初始化器
        application.setListeners();//设置自定义的监听器
        application.setLogStartupInfo(true);//设置是否显示应用启动信息
        application.setRegisterShutdownHook(true);//设置是否添加关闭狗子
        application.setResourceLoader();//设置自定义的资源加载器
        application.setSources();//设置自定义的资源
        application.run(args);//启动应用
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_34268579/article/details/87166137