spring 在Sping Boot Banner中显示自定义变量

aiazj4mn  于 12个月前  发布在  Spring
关注(0)|答案(3)|浏览(136)

我想在我的Spring Boot 应用程序自定义Spring Boot 横幅。我想在我的横幅显示自定义变量。

class AppProfile {

    String title;
    String version;

    //getter-setter and all other stafs
    ......................
    ......................
    ......................

}

字符串
如何在Sping Boot Banner中显示标题和版本?

wf82jlnq

wf82jlnq1#

这是一种方法:

@SpringBootApplication
public class BootApplication extends SpringBootServletInitializer {

    public static void main(final String[] args) {
        SpringApplication springApplication = new SpringApplication(BootApplication.class);
        springApplication.setBanner(new CustomBanner());
        springApplication.run(args);
    }

    private static class CustomBanner implements Banner {
        //@formatter:off
        private static final String[] BANNER = {
                "  ______          ",
                " |  ____|         ",
                " | |__ ___   ___  ",
                " |  __/ _ \\ / _ \\ ",
                " | | | (_) | (_) |",
                " |_|  \\___/ \\___/ ",
                "                  "
        };
        //@formatter:on

        private static final String SPRING_BOOT = " :: Spring Boot :: ";

        @Override
        public void printBanner(final Environment environment, final Class<?> sourceClass, final PrintStream printStream) {
            String padding = "";
            for (String line : BANNER) {
                printStream.println(line);
            }
            AppProfile appProfile = new AppProfile();
            printStream.println(AnsiOutput.toString(AnsiColor.YELLOW, appProfile.title, AnsiColor.DEFAULT, padding, AnsiStyle.FAINT,
                    appProfile.version));
            printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT, AnsiColor.DEFAULT, padding, AnsiStyle.FAINT,
                    SpringBootVersion.getVersion()));
            printStream.println();
        }

    }

    private static class AppProfile {
        public String title = " :: Foo App :: ";
        public String version = "4.2.12.123";
    }
}

字符串

iugsix8n

iugsix8n2#

Spring提供了通过在应用程序中添加banner.txt文件来覆盖banner的功能。将您的自定义徽标banner文本添加到/src/main/resources/banner. txt文件。
下面是一个很酷的工具来生成横幅文本。
https://devops.datenkollektiv.de/banner.txt/index.html

5vf7fwbs

5vf7fwbs3#

您可以在pom.xml中添加插件

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                        <delimiters>
                            <delimiter>#</delimiter>
                        </delimiters>
                        <resources>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>*.yml</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>false</filtering>
                                <excludes>
                                    <exclude>*.yml</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

字符串
并在资源下创建一个banner.txt,

,------.,--.   ,--.  ,---.  ,--.   ,--.,------. ,--.   ,------. 
|  .---' \  `.'  /  /  O  \ |   `.'   ||  .--. '|  |   |  .---' 
|  `--,   .'    \  |  .-.  ||  |'.'|  ||  '--' ||  |   |  `--,  
|  `---. /  .'.  \ |  | |  ||  |   |  ||  | --' |  '--.|  `---. 
`------''--'   '--'`--' `--'`--'   `--'`--'     `-----'`------' 
                                                                
${application.title} ${application.version}


并在您的application.yml文件中Assert,您具有以下属性

application:
  title: Hello
  version: V1

相关问题