Springboot基础配置(banner,配置文件等)

1. 定制Banner

Spring Boot项目在启动的时候会有一个默认的启动图案:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.0)

我们可以把这个图案修改为自己想要的。在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。ASCII图案可通过网站https://www.bootschool.net/ascii/一键生成,比如输入JEECG生成图案后复制到banner.txt,启动项目,控制台输出如下:

image-20220601145357841

banner也可以关闭,在main方法中:

2. 全局配置文件

在src/main/resources目录下,Spring Boot提供了一个名为application.properties的全局配置文件,可对一些默认配置的配置值进行修改。

附:application.properties中可配置所有官方属性

2.1 自定义属性值

Spring Boot允许我们在application.properties下自定义一些属性,比如:

cc.blog.name=cc's blog
cc.blog.title=Spring Boot

定义一个BlogProperties Bean,通过@Value("${属性名}")来加载配置文件中的属性值:

@Component
@Data
public class BlogProperties {

    @Value("${cc.blog.name}")
    private String name;

    @Value("${cc.blog.title}")
    private String title;

}

编写IndexController,注入该Bean:

@RestController
public class IndexController {
    
    @Autowired
    private BlogProperties blogProperties;

    @RequestMapping("/")
    String index() {
        return blogProperties.getName() + "——" + blogProperties.getTitle();
    }
}

启动项目,访问http://localhost:8080,页面显示如下:

image-20220601150625474

在属性非常多的情况下,也可以定义一个和配置文件对应的Bean:

@ConfigurationProperties(prefix = "cc.blog")
@Data
public class ConfigBean {
    
    private String name;
    
    private String title;
    
}

通过注解@ConfigurationProperties(prefix="cc.blog")指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名一一对应。

除此之外还需在Spring Boot入口类加上注解@EnableConfigurationProperties({ConfigBean.class})来启用该配置:

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class SpringbootConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootConfigApplication.class, args);
    }

}

之后便可在IndexController中注入该Bean,并使用了:

@RestController
public class IndexController {

    @Autowired
    private BlogProperties blogProperties;

    @Autowired
    private ConfigBean configBean;

    @RequestMapping("/")
    String index() {
        return configBean.getName() + "——" + configBean.getTitle();
    }
}

2.2 属性间的引用

在application.properties配置文件中,各个属性可以相互引用,如下:

cc.blog.name=cc's blog
cc.blog.title=Spring Boot
cc.blog.wholeTitle=${cc.blog.name}--${cc.blog.title}
# 3. 自定义配置文件

除了可以在application.properties里配置属性,我们还可以自定义一个配置文件。在src/main/resources目录下新建一个test.properties:

test.name=jack
test.age=25

定义一个对应该配置文件的Bean:

@Configuration
@ConfigurationProperties(prefix = "test")
@PropertySource("classpath:test.properties")
@Component
@Data
public class TestConfigBean {
    private String name;
    
    private int age;
}

注解@PropertySource("classpath:test.properties")指明了使用哪个配置文件。要使用该配置Bean,同样也需要在入口类里使用注解@EnableConfigurationProperties({TestConfigBean.class})来启用该配置。

4. 通过命令行设置属性值

在运行Spring Boot jar文件时,可以使用命令java -jar xxx.jar --server.port=8081来改变端口的值。这条命令等价于我们手动到application.properties中修改(如果没有这条属性的话就添加)server.port属性的值为8081。

如果不想项目的配置被命令行修改,可以在入口文件的main方法中进行如下设置:

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(Application.class);
    app.setAddCommandLineProperties(false);
    app.run(args);
}

5. 使用xml配置

虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解@ImportResource({"classpath:some-application.xml"})来引入xml配置文件。

6. Profile配置

Profile用来针对不同的环境下使用不同的配置文件,多环境配置文件必须以application-{profile}.properties的格式命,其中{profile}为环境标识。比如定义两个配置文件:

  • application-dev.properties:开发环境

    server.port=8080
    
  • application-prod.properties:生产环境

    server.port=8081
    

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

如:spring.profiles.active=dev就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令java -jar xxx.jar --spring.profiles.active={profile}切换不同的环境配置。

<think>嗯,用户想了解Spring Boot中的Banner。首先,我需要回忆一下Spring Boot基础知识,Banner是在启动时显示的那个ASCII艺术字,对吧?那得先确认这个信息是否正确。然后,我应该说明Banner的作用,可能包括定制化启动界面,或者用来显示版本信息之类的。 接下来,用户可能想知道如何自定义Banner。需要分步骤说明,比如在resources目录下放置banner.txt文件,或者使用图片。但图片的话可能需要其他依赖,比如Spring Boot的某个版本是否支持?或者是否需要配置额外的属性? 然后,可能需要提到支持的格式,比如文本、图片(.gif, .jpg, .png),以及对应的配置属性spring.banner.image.location。还要讲一下如何关闭Banner,通过设置spring.main.banner-mode=off或者在代码中设置。 另外,高级定制的话,可能需要实现Banner接口,并注册到SpringApplication中。这部分需要详细说明步骤,可能用户中的开发者会需要这样的信息。 还要注意用户可能的常见问题,比如Banner不显示的原因,可能是配置错误或者文件位置不对。或者如何更改Banner的颜色,这可能需要使用ANSI转义码,但要确保控制台支持。 需要检查是否有过时的信息,比如不同Spring Boot版本中Banner配置方式是否有变化。例如,早期版本可能有些不同的配置属性,但现在可能统一了。确保提到的内容都是当前通用的做法。 此外,示例代码部分要准确,比如在application.properties中设置属性,或者在main方法里使用setBannerMode()。这部分代码必须正确,不能有语法错误。 最后,提醒用户Banner虽然是个小功能,但在实际项目中可以用来展示环境、版本等信息,增加专业性。但也要注意不要滥用,保持简洁。 总结结构应该是先介绍Banner是什么,然后如何自定义,包括文本、图片、关闭,接着高级定制方法,最后注意事项和总结。这样结构清晰,逐步深入,帮助用户理解和应用。</think>在Spring Boot中,Banner是在应用启动时控制台显示的ASCII艺术字(如"SPRING"字样)。以下是关于Banner的详细介绍: ### 一、Banner的作用 1. **标识应用**:展示项目名称或公司信息 2. **版本显示**:可集成Spring Boot版本、JDK版本等信息 3. **个性化定制**:通过ASCII艺术增强启动体验 ### 二、自定义Banner #### 1. 基础方式 在`resources`目录下创建: - `banner.txt`:文本格式 - `banner.jpg`/`banner.png`/`banner.gif`:图片格式 示例`banner.txt`内容: ``` ___ _ __ _ __(_)_ __ __ _ / _ \ '_ \| '__| | '_ \ / _` | | __/ |_) | | | | | | | (_| | \___| .__/|_| |_|_| |_|\__, | |_| |___/ Spring Boot Version: ${spring-boot.version} ``` #### 2. 配置属性 在`application.properties`中设置: ```properties # 图片Banner配置 spring.banner.image.location=classpath:banner.png spring.banner.image.width=50 # 字符宽度 spring.banner.image.height=20 # 字符高度 # 文本Banner配置 spring.banner.location=classpath:banner.txt # 关闭Banner spring.main.banner-mode=off ``` #### 3. 动态变量 支持预定义变量: - `${application.version}`:`pom.xml`中的版本 - `${spring-boot.version}`:Spring Boot版本 - `${AnsiColor.BRIGHT_RED}`:ANSI颜色控制 ### 三、高级定制 实现`Banner`接口: ```java public class CustomBanner implements Banner { @Override public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) { out.println(">> Custom Banner <<"); } } // 在main方法中设置 public static void main(String[] args) { new SpringApplicationBuilder() .banner(new CustomBanner()) .run(args); } ``` ### 四、注意事项 1. **编码问题**:确保banner.txt使用UTF-8编码 2. **颜色支持**:需控制台支持ANSI转义码 3. **性能影响**:复杂Banner可能略微延长启动时间 4. **环境区分**:可通过Profile设置不同环境的Banner ### 五、关闭Banner 1. 配置文件方式: ```properties spring.main.banner-mode=off ``` 2. 编程方式: ```java new SpringApplicationBuilder() .bannerMode(Banner.Mode.OFF) .run(args); ``` ### 六、最佳实践 1. 生产环境建议包含版本信息 2. 开发环境可启用彩色Banner 3. 使用在线工具生成ASCII艺术字(如:https://www.bootschool.net/ascii) 通过合理使用Banner,可以提升应用的专业性和可维护性,但需注意不要过度设计影响启动日志的可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

、楽.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值