SpringBoot一些基础配置
定制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一些基础配置的更多相关文章
- 二、SpringBoot基础配置
目录 2.1 @SpringBootApplication 2.3 服务器配置 2.4 修改启动banner 小结 2.1 @SpringBootApplication 从上篇文章中知道@Spring ...
- 30分钟了解Shiro与Springboot的多Realm基础配置
写在前面的话: 我之前写过两篇与shiro安全框架有关的博文,居然能够广受欢迎实在令人意外.说明大家在互联网时代大伙对于安全和登录都非常重视,无论是大型项目还是中小型业务,普遍都至少需要登录与认证的逻 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战
笔记 1.SpringBoot多环境配置介绍和项目实战(核心知识) 简介:SpringBoot介绍多环境配置和使用场景 1.不同环境使用不同配置 例如数据库配置,在开发的时候, ...
- Spring Boot 基础配置
之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...
- springboot mvc自动配置(三)初始化mvc的组件
所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...
- 关于SpringBoot的自动配置和启动过程
一.简介 Spring Boot简化了Spring应用的开发,采用约定大于配置的思想,去繁从简,很方便就能构建一个独立的.产品级别的应用. 1.传统J2EE开发的缺点 开发笨重.配置繁多复杂.开发效率 ...
- 【SpringBoot】SpringBoot的基础,全面理解bean的生命周期
前言 前段时间直接上手使用springboot开发了一个数据平台的后台部分,但是自身对于springboot的原理和过程还不是很清晰,所以反过来学习下springboot的基础. 大家都知道sprin ...
- SpringBoot之基础入门-专题一
SpringBoot之基础入门-专题一 一.Spring介绍 1.1.SpringBoot简介 在初次学习Spring整合各个第三方框架构建项目的时候,往往会有一大堆的XML文件的配置,众多的dtd或 ...
- Springboot MVC 自动配置
Springboot MVC 自动配置 官方文档阅读 https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#w ...
随机推荐
- lnmp环境搭建:Centos7 + Nginx1.12.2 + Mysql-5.6.38 + PHP7.2.0
https://blog.csdn.net/ty_hf/article/details/50622888
- POJ 3617 Best Cow Line 字典序最小
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...
- Introduction to Differential Equations,Michael E.Taylor,Page 3,4 注记
此文是对 [Introduction to Differential Equations,Michael E.Taylor] 第3页的一个注记.在该页中,作者给了微分方程$$\frac{dx}{dt} ...
- 利用VLC解码youtube视频链接
一.需求 现在有youtube视频播放链接(I),需要得到可以直接播放的视频链接(O). 输入链接(I): https://www.youtube.com/watch?v=xAsjRRMMg_Q 输出 ...
- java后端导出excel
最近工作中需要导出excel.这次机智一点做个笔记,顺便写了一个比较通用的工具类.自然目前不能生成java实体类属性嵌套多次的这种没办法导出了,后续有需要的时候我再改改. 首先,java后端导出exc ...
- 五、RabbitMQ Java Client基本使用详解
Java Client的5.x版本系列需要JDK 8,用于编译和运行.在Android上,仅支持Android 7.0或更高版本.4.x版本系列支持7.0之前的JDK 6和Android版本. 加入R ...
- linux下如何查看服务器的硬件配置信息
性能测试时一定要确定测试环境和的硬件配置.软件版本配置,保证和线上一致,才更接近真实环境. 那么linux下如何查看服务器的硬件配置信息?? 一.查看cpu信息 1.所有信息 lscpu [root@ ...
- centos 6.* 修改时间
一.查看Centos的时区和时间 1.使用date命令查看Centos时区 [root@VM_centos ~]# date -R Mon, 26 Mar 2018 19:14:03 +0800 2. ...
- yield解析
1.yield可以用来为一个函数返回值塞数据 代码: def addlist(alist): for i in alist: alist = [, , , ] for x in addlist(ali ...
- sql语句查询成绩表各科前三名
--语法形式: ROW_NUMBER() OVER(PARTITION BY COL1 ORDER BY COL2) --解释: 根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示 ...