定制banner

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

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

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

 _       _________
( ( /|\__ __/|\ /|
| \ ( | ) ( | ) ( |
| \ | | | | | | | |
| (\ \) | | | | | | |
| | \ | | | | | | |
| ) \ |___) (___| (___) |
|/ )_)\_______/(_______)

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

public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setBannerMode(Mode.OFF);
app.run(args);
}

全局文件配置

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

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

自定义属性值

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

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

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

@Component
@Data
public class BlogProperties { @Value("${mrbird.blog.name}")
private String name; @Value("${mrbird.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,页面显示如下:

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

@ConfigurationProperties(prefix="mrbird.blog")
public class ConfigBean {
private String name;
private String title;
// get,set略
}

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

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

@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

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

@RestController
public class IndexController {
@Autowired
private ConfigBean configBean; @RequestMapping("/")
String index() {
return configBean.getName()+"——"+configBean.getTitle();
}
}

属性间的引用

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

mrbird.blog.name=mrbird's blog
mrbird.blog.title=Spring Boot
mrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title}

自定义配置文件

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

test.name=KangKang
test.age=

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

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

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

通过命令行设置属性值

在运行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);
}

使用xml配置

虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解

@ImportResource({"classpath:some-application.xml"})来引入xml配置文件。

Profile配置

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

application-dev.properties:开发环境

server.port=

application-prod.properties:生产环境

server.port=

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

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

SpringBoot一些基础配置的更多相关文章

  1. 二、SpringBoot基础配置

    目录 2.1 @SpringBootApplication 2.3 服务器配置 2.4 修改启动banner 小结 2.1 @SpringBootApplication 从上篇文章中知道@Spring ...

  2. 30分钟了解Shiro与Springboot的多Realm基础配置

    写在前面的话: 我之前写过两篇与shiro安全框架有关的博文,居然能够广受欢迎实在令人意外.说明大家在互联网时代大伙对于安全和登录都非常重视,无论是大型项目还是中小型业务,普遍都至少需要登录与认证的逻 ...

  3. 小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战

    笔记 1.SpringBoot多环境配置介绍和项目实战(核心知识)     简介:SpringBoot介绍多环境配置和使用场景 1.不同环境使用不同配置         例如数据库配置,在开发的时候, ...

  4. Spring Boot 基础配置

    之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...

  5. springboot mvc自动配置(三)初始化mvc的组件

    所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...

  6. 关于SpringBoot的自动配置和启动过程

    一.简介 Spring Boot简化了Spring应用的开发,采用约定大于配置的思想,去繁从简,很方便就能构建一个独立的.产品级别的应用. 1.传统J2EE开发的缺点 开发笨重.配置繁多复杂.开发效率 ...

  7. 【SpringBoot】SpringBoot的基础,全面理解bean的生命周期

    前言 前段时间直接上手使用springboot开发了一个数据平台的后台部分,但是自身对于springboot的原理和过程还不是很清晰,所以反过来学习下springboot的基础. 大家都知道sprin ...

  8. SpringBoot之基础入门-专题一

    SpringBoot之基础入门-专题一 一.Spring介绍 1.1.SpringBoot简介 在初次学习Spring整合各个第三方框架构建项目的时候,往往会有一大堆的XML文件的配置,众多的dtd或 ...

  9. Springboot MVC 自动配置

    Springboot MVC 自动配置 官方文档阅读 https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#w ...

随机推荐

  1. OAuth 2.0安全案例回顾

    转载自:http://www.360doc.com/content/14/0311/22/834950_359713295.shtml 0x00 背景 纵观账号互通发展史,可以发现OAuth比起其它协 ...

  2. GB35658较796新增检测项部标平台

    GB35658较796新增检测项部标平台总共有113项,总结归类如下:1    报表导出    支持excel格式的报表导出    对查询.统计报表提供excel格式的报表导出    必选:    2 ...

  3. [C#] 动态指定泛型类型

    前言 今天为了程序能写好看一点,一直在纠结怎么指定动态泛型, 但是想想实用性好像不太大,可是把这技术忘掉太可惜XD 还是记录下来,以防忘记 以下程序范例 类 cs 123456789101112131 ...

  4. 如何修改tomcat名称

    修改tomcat名称为  Tomcat-jx-1

  5. ExecuteScalar()方法的使用

    ExecuteScalar()方法的作用是: 执行查询,并返回查询所返回的结果集中第一行的第一列.所有其他的列和行将被忽略. 1.返回的是一个object类型,也就是说是所有数据类型的基类,可根据se ...

  6. Python连接SQLServer2000

    http://www.pymssql.org/en/stable/pymssql_examples.html 实例 import pymssql # 获取连接 conn = pymssql.conne ...

  7. python后端面试第三部分:数据储存与缓存相关--长期维护

    1. 列举常见的关系型数据库和非关系型都有哪些?2. MySQL常见数据库引擎及比较?3. 简述数据三大范式?4. 什么是事务?MySQL如何支持事务?5. 简述数据库设计中一对多和多对多的应用场景? ...

  8. django框架进阶-admin-长期维护

    ###############    django--admin的使用    ################ # django后台管理: # 第一步: # 在settings文件中修改语言和时区 L ...

  9. Jumpserver 一键部署(支持离线安装)

    1.教程介绍1.1::通过本教程起到抛砖引玉效果,希望各位喜爱Jumpserver堡垒机的朋友受益良多. 1.2::以下提供的任何软件仅供学习交流使用. 2.下载链接2.1::centos_1810最 ...

  10. 吴裕雄--天生自然python学习笔记:案例:用正则表达式查找邮件账号

    抓取万水书苑“ ht甲://www.w sbookshow.com/ ”网站中的所有 E-mai l 账号. import requests,re regex = re.compile('[a-zA- ...