1. 配置文件简介

spring boot使用一个全局配置文件:application.properties或者application.yml,放置在src/main/resources目录下或者类路径的/config目录下。

application.properties是我们熟知的键值对配置文件:

 
application.properties

application.yml是yaml语言的配置文件,yaml是一种以数据为中心的语言,在配置数据的时候具有面向对象的特征。我们以后的代码都会使用yml格式的配置文件。:

 
application.yml

spring boot 的全局配置文件的作用是对一些默认配置的配置进行修改。后面我们会详细的讲解。

2. pom配置文件(starter)

pom,是maven的配置文件,gradel也差不多,因为我一直在用maven,所以就讲maven了。

为了解决大型项目以及一些常用组件jar包过多的问题,spring boot 为我们提供了许多starter pom,每一个pom都已经添加好了对应的依赖,以往我们需要写茫茫多的dependence,但是现在只需要一个starter就可以了,并且spring boot 还为它们提供了默认的配置和自动配置的bean。

2.1 官方starter

  • 2.1.1 应用程序的starters
名称 描述
spring-boot-starter 核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator 生产准备的特性,用于帮我们监控和管理应用
spring-boot-starter-amqp 对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop 对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch 对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors 对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch 对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire 对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa 对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb 对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest 对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr 对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker 对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates 对Groovy模板引擎的支持
spring-boot-starter-hateoas 对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq 对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration 对普通spring-integration模块的支持
spring-boot-starter-jdbc 对JDBC数据库的支持
spring-boot-starter-jersey 对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos 对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix 对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mail 对javax.mail的支持
spring-boot-starter-mobile 对spring-mobile的支持
spring-boot-starter-mustache 对Mustache模板引擎的支持
spring-boot-starter-redis 对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-security 对spring-security的支持
spring-boot-starter-social-facebook 对spring-social-facebook的支持
spring-boot-starter-social-linkedin 对spring-social-linkedin的支持
spring-boot-starter-social-twitter 对spring-social-twitter的支持
spring-boot-starter-test 对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf 对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity 对Velocity模板引擎的支持
spring-boot-starter-web 对全栈web开发的支持, 包括Tomcat和spring-webmvc
spring-boot-starter-websocket 对WebSocket开发的支持
spring-boot-starter-ws 对Spring Web服务的支持
  • 2.1.2 spring boot生产准备的starters:
名称 描述
spring-boot-starter-actuator 添加生产准备特性,比如指标和监控
spring-boot-starter-remote-shell 添加远程ssh shell支持
  • 2.1.3 排除或交换具体技术方面的starters
名称 描述
spring-boot-starter-jetty 导入Jetty HTTP引擎(作为Tomcat的替代)
spring-boot-starter-log4j 对Log4J日志系统的支持
spring-boot-starter-logging 导入Spring Boot的默认日志系统
spring-boot-starter-tomcat 导入Spring Boot的默认HTTP引擎
spring-boot-starter-undertow 导入Undertow HTTP引擎(作为Tomcat的替代)

2.2 非官方starter

也有一些第三方为spring boot 提供了starter:

  • Handlebars
  • Vaadin
  • Apache Camel
  • WRO4J
  • Spring Batch(高级用法)
  • HDIV
  • Jade Templates(Jade4J)
  • Actitivi

这些都可以在github上找到,感兴趣的可以去搜一下。

3. xml配置文件

spring boot 提倡零配置,也就是无xml配置,但是在实际项目中,可能有一些要求必须使用xml配置,这时我们可以使用spring提供的@ImportResource 来加载xml配置,比如:

@ImportResource({"classpath:some-context.xml","classpath:other-context.xml"})

4. 命令行参数配置

上一篇文章中也有提到,spring boot 项目是可以打成jar包的,然后通过java -jar xxx.jar 来执行,我们都知道,main函数是可以接受参数的,同样,spring boot的main函数也可以。

我们可以通过下面的命令来修改Tomcat的端口号:

java -jar xxx.jar --server.port=9090

5. 自定义属性配置

5.1 普通配置

在写spring项目的时候,我们可以在properties文件中定义一个变量,然后再代码中通过 @PropertySource 指明 properties 文件的位置,然后通过@Value注入相应的值。

在spring boot 中我们同样可以自定义一个变量,因为spring boot中的全局配置文件默认在一个目录下,所以我们可以直接用@Value注入值。

比如,在上一篇文章的demo项目中,我们可以在application.yml文件中这样定义:

key:
hello: hello world

修改DemoApplication.java代码为:

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@RestController
public class DemoApplication { @Value("${key.hello}")
private String hello; public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @RequestMapping("/hello")
public String hello(){
return hello;
}
}

运行项目,在浏览器中访问http://localhost:8080/hello

 
结果

值被正常的注入进去了。

5.2 类型安全的配置

上面的例子中,使用@Value注入每个配置在实际的应用中会显得格外的麻烦,因为配置一般会有很多,用上面的方式就要写好多@Value,并且如果在多个类文件中都使用这个值的话,工作量会更大。所以,需要一种更好的方式。并且,这种方式是有安全隐患的,比如我需要的是一个long类型,但是注入的却是String,这个时候就报错了:

Failed to convert value of type 'java.lang.String' to required type 'long';

spring boot 提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个bean及其属性关联,从而实现类型安全的配置。

我们来看一下:

application.yml:

key:
name: cleverfan
sex: man

在demo项目中创建一个class: Person.java

@Component
@ConfigurationProperties(prefix = "key")
public class Person {
private String name;
private String sex; public void setName(String name) {
this.name = name;
} public void setSex(String sex) {
this.sex = sex;
} public String getName() {
return name;
} public String getSex() {
return sex;
}
}

修改DemoApplication.java :

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@RestController
public class DemoApplication { @Autowired
private Person person; public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @RequestMapping("/hello")
public String hello(){
return person.getName() + ": " + person.getSex();
}
}

启动项目,访问http://localhost:8080/hello

 
结果

回到刚刚的问题,如果这个时候我需要的是long,但是给的是String怎么办呢?这里给出一个很简单的处理方式:

Person.java

@Component
@ConfigurationProperties(prefix = "key")
public class Person {
private String name;
private long sex; public void setName(String name) {
this.name = name;
} public void setSex(String sex) {
try{
this.sex = Long.valueOf(sex);
}catch (Exception e){
this.sex = 0L;
} } public String getName() {
return name;
} public long getSex() {
return sex;
}
}

yml配置文件不变,我们传递的是字符串,这次重启项目,访问http://localhost:8080/hello

 
结果

这样就可以简单的保证数据的安全了。

6. 日志配置

spring boot 支持Java Util Loggin、Log4J、Log4J2和LogBack作为日志框架,无论使用哪种日志框架,spring boot已为当前使用日志框架的控制台输出及文件输出做好了配置。

默认情况下,spring boot 使用 Logback作为日志框架。可以在配置文件中(yml)修改默认配置文件的配置:

  • 配置日志级别:
logging:
level: debug
  • 配置日志文件
logging:
org:
springframework:
web: debug

7. Profile配置

Profile 是 spring 用来针对不同的环境对不同的配置提供支持的,比如生成环境,测试环境和开发环境。全局 Profile 使用application-{profile}.yml(例如application-prod.yml)命名。

下面我们做一个演示,我们把demo项目分为生产(prod)和开发(dev)环境,生产环境下端口号为8000,开发环境下为8888。

我们在demo项目的application.yml文件的同级目录下创建两个文件:
application-prod.yml:

server:
port: 8000

application-dev.yml:

server:
port: 8888

目录结构是这样的:

 
目录结构

application.yml

key:
name: cleverfan
sex: man logging:
level: debug spring:
profiles:
active: dev

启动项目:

 
dev

端口号是8888

修改application.yml:

spring:
profiles:
active: prod

重新启动项目:

 
prod

端口号的8000

证明我们的配置生效了。


作者:cleverfan
链接:https://www.jianshu.com/p/8019aa646414
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

springboot_2的更多相关文章

  1. 【Mysql】SpringBoot_2.1.0+Druid_1.1.10 配置数据源监控服务Yml格式

    访问地址:localhost:8080/druid 按照这个方法和版本配置没问题 版本或高或低可能会出现不兼容 1.添加依赖 <dependency> <groupId>com ...

  2. JEECG-Boot 项目介绍——基于代码生成器的快速开发平台(Springboot前后端分离)

    Jeecg-Boot 是一款基于代码生成器的智能开发平台!采用前后端分离架构:SpringBoot,Mybatis,Shiro,JWT,Vue&Ant Design.强大的代码生成器让前端和后 ...

  3. java开源项目学习

    http://jeecg-boot.mydoc.io/ 在线文档已切换至新地址: http://doc.jeecg.com Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平 ...

随机推荐

  1. Prometheus基于consul自动发现监控对象 https://www.iloxp.com/archive/11/

      Prometheus 监控目标为什么要自动发现 频繁对Prometheus配置文件进行修改,无疑给运维人员带来很大的负担,还有可能直接变成一个“配置小王子”,即使是配置小王子也会存在人为失误的情况 ...

  2. GridLayout: GridLayout使用简介(转)

    Android 布局之GridLayout 1 GridLayout简介 GridLayout是Android4.0新提供的网格矩阵形式的布局控件. GridLayout的继承关系如下:java.la ...

  3. Dart对象和类

    /* 面向对象编程(OOP)的三个基本特征是:封装.继承.多态 封装:封装是对象和类概念的主要特性.封装,把客观事物封装成抽象的类,并且把自己的部分属性和方法提供给其他对象调用, 而一部分属性和方法则 ...

  4. osg编译日志-release

    1>------ 已启动生成: 项目: ZERO_CHECK, 配置: Release x64 ------1> Checking Build System1> CMake does ...

  5. Python机器学习实践指南pdf (中文版带书签)、原书代码、数据集

    Python机器学习实践指南 目 录 第1章Python机器学习的生态系统 1 1.1 数据科学/机器学习的工作 流程 2 1.1.1 获取 2 1.1.2 检查和探索 2 1.1.3 清理和准备 3 ...

  6. spring 加载属性(properties)文件

    在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties 代码清单:databas ...

  7. Python - Django - ORM 一对一表结构

    当一张表的某一些字段查询的比较频繁,另外一些字段查询的不是特别频繁,可以把不怎么常用的字段 单独拿出来做成一张表,然后用一对一的表关联起来 这样既保证数据都完整的保存下来,又能保证检索更快 model ...

  8. 随机采样一致算法RANSAC

    A project to learn line, circle and ellipse detection in 2d images: https://github.com/Yiphy/Ransac- ...

  9. Python第一阶段02

    1.模块: import sys print(sys.path) # 打印环境变量 print(sys.argv) # 打印当前文件绝对路径 # print(sys.argv[]) import os ...

  10. 使用docker搭建FastDFS文件系统

    1.首先下载FastDFS文件系统的docker镜像 docker search fastdfs 2.使用docker镜像构建tracker容器(跟踪服务器,起到调度的作用): docker run ...