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 ...
随机推荐
- netstat - 系统信息
netstat - 系统信息 注意:如果是勘验或者验证漏洞,需要验证netstat程序的完整性(netstat程序是否被修改过). # 老版本的CentOS中会自带这个软件包,新版的7有的时候需要单独 ...
- python学习笔记(22)-os文件操作模块
疑问: 如果打开操作一个文件,是用绝对路径好还是相对路径好? os模块,在lib下面,可以直接引入的,直接使用import. 一.新建一个目录,新建一个文件夹 import os #新建一个文件夹 o ...
- 【UML】
静态:类图,包图,部署图,构件图,对象图 行为:用例图,活动图,顺序图,状态图,交互图 [类图] http://www.uml.org.cn/oobject/201104212.asp [对象图] h ...
- iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码
iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...
- 吴裕雄--天生自然 HADOOP大数据分布式处理:安装配置Tomcat服务器
下载链接:https://tomcat.apache.org/download-80.cgi tar -zxvf apache-tomcat-8.5.42.tar.gz -C /usr/local/s ...
- discussion|局限性|解释|猜测|前作与同行
讨论是整篇论文的精华和灵魂,考查作者的文献积累量和对所研究内容的理解深度,作者需要阐述为什么结果是重要的,内容包括理论.应用.在其他其他领域的作用及应用,阐述时要求直接明确. 具体而言,首先概述最重要 ...
- OSI体系结构(七层)
OSI体系结构,意为开放式系统互联.国际标准组织(国际标准化组织)制定了OSI模型.这个模型把网络通信的工作分为7层,分别是物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 1至4层被认为 ...
- 求求你,下次面试别再问我什么是 Spring AOP 和代理了!
https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9403056301388627935% ...
- python初认函数
今日所得 函数基本使用 函数的参数 函数的返回值 # 函数内要想返回给调用者值 必须用关键字return """ 不写return 只写return 写return No ...
- Mr.Yu
在linux下搭建Git服务器 git服务器环境 服务器 CentOS7 + git(version 1.8.3.1)客户端 Windows10 + git(version 2.16.0.window ...