定制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. zabbix监控Linux服务器丢包率

    http://www.ttlsa.com/zabbix/zabbix-simple-checks/  这个文章看了,还没有实践 1.先创建监控项,键值如下 icmppingloss[<121.1 ...

  2. WMS出库单重复

    发货通知单?WMS备货单选项勾选 不自动复制?新增?

  3. [LC] 1048. Longest String Chain

    Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...

  4. C++ 类中使用dllimport和dllexport

    在Windows平台下: 您可以使用dllimport或dllexport属性声明C ++类.这些形式意味着导入或导出整个类.以这种方式导出的类称为可导出类. 以下示例定义可导出的类.导出其所有成员函 ...

  5. day27-控制台输出彩色文字

    格式:\033[显示方式;前景色;背景色m 说明:显示方式           意义-------------------------  0             终端默认设置  1         ...

  6. vue_webpack初始化项目

    整体架构:此处npm安装过于缓慢,因此使用的是淘宝的镜像cnpm vue+webpack 初始化项目:1.安装vue: cnpm install vue 检验版本: vue -V2.创建一个vue项目 ...

  7. A4988驱动42步进电机

    A4988步进电机驱动器驱动控制42步进电机速度,步进电机调速,调节驱动电流       1  A4988步进电机驱动器简介 方便使用,是我们这些用户最想要的,固有的名词和深入介绍在这就不多说了,您可 ...

  8. be accustomed to doing|actual |acute|adapt |

    Sometimes you've got to play a position that you're not accustomedto for 90 minutes, " he said. ...

  9. Python 装饰器 多装饰器同时装饰一个函数 多参数函数

    装饰器是在不修改源代码的情况下,使用装饰器增加原函数的功能. 在软件开发中有一个原则——"开放-封闭",简单地说就是已经实现的功能不允许被修改,但可以被扩展. 封闭:已经实现的功能 ...

  10. Ubuntu16.04使用sublime text3编写C语言后,实现编译并自动调用bash终端运行程序

    实现编译并自动调用bash运行程序只需要新建自己的.build文件就OK 依次打开: tools->building system->new building system 后,把下面的内 ...