一、概述

  application.properties就是springboot的属性配置文件

  在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配置足以满足正常的功能开发。

  除此之外,还有application.yml形式的配置文件;对比如下;

server.port=8080
server.session-timeout=30
server.context-path=
server.tomcat.max-threads=0
server.tomcat.uri-encoding=UTF-8 spring.datasource.url = jdbc:mysql://localhost:3306/spring
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

application.properties

server:
port: 8080
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8 spring:
datasource:
url : jdbc:mysql://localhost:3306/springboot
username : root
password : root
driverClassName : com.mysql.jdbc.Driver
jpa:
database : MYSQL
show-sql : true
hibernate:
ddl-auto : update
naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:

application.yml

  默认生成的为application.properties

  # 补充springboot日志详解

二、修改默认配置

  spring-boot自带了很多默认配置,所有默认配置可以参考官方文档:点击查看所有默认配置

  示例:修改端口和context-path:

server.port=8081
server.context-path=/demo

  // 新版的IDEA写此配置文件也会给出对应默认配置名称的提示,赞!

  

  当然,通过jar方式运行也可以使用--进行参数设置,这是专门用于对属性文件进行配置的:

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

  当然,properties写起来比较费事,我们推荐的是yml(压ml)形式的配置:

    这里必须提醒,yml配置文件分割处必须有空格:当然IDEA也会帮我们检查yml语法,正确的配置是会高亮的,yml文件本身也会有特殊图标显示的

    

    这里我们就把默认的properties删除了,使用更加简洁的yml配置:

server:
port: 8082
context-path: /demo

    如果已经是properties,可以通过在线工具转换http://www.toyaml.com/

    效果:

    

三、自定义属性配置

  1.在yml文件中定义:

salary: 10010
name: jiangbei

  2.使用@Value注解读取值:

    使用@Value的类如果被其他类作为对象引用,必须要使用注入的方式,而不能new

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* 测试demo的controller
*
* @author zcc ON 2018/2/8
**/
@RestController
public class HelloController {
@Value("${name}")
private String name;
@Value("${salary}")
private Integer salary; @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "name:" + name + " salary:" + salary;
}
}

  页面效果:

  

  // 可以看到,配置文件到属性的过程,会自动进行类型转换

    读取多级属性:

name:
first_name: jiangbei
last_name: z
  @Value("${name.first_name}")
private String fname;
@Value("${name.last_name}")
private String lname;

    属性中引用其他属性:类似shell变量

name:
first_name: jiangbei
last_name: z
full_name: "full name: ${name.first_name} ${name.last_name}"

    使用bean进行属性映射

    需要特别注意的是bean上面的两个注解:@Componet和@ConfigurationProperties,通过prefix来匹配前缀(类似分组思想),当然也可以

通过 @ConfigurationProperties(prefix = "master.ds",locations = "classpath:application.properties")  的形式,指定配置文件

person:
name: jiangbei
age: 18
sex: M
package com.example.demo.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* 映射属性配置的bean
*
* @author zcc ON 2018/2/8
**/
@ConfigurationProperties(prefix = "person")
@Component
public class PersonProp {
private String name;
private Integer age;
private String sex;
// getter setter未列出
}
@RestController
public class HelloController {
@Autowired
private PersonProp person; @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return person.getName();
}
}

    多配置文件切换:

      例如生产和开发的配置文件需要切换,那我们只要定义application-xxx.yml,然后在application.yml里面使用spring.profile.active切换即可!

    

    在application.yml中进行配置:

spring:
profiles:
active: dev

   这个时候再在application.yml文件里面写配置的话,dev里面也是可以直接用到的!

   更多属性特征与使用,参考: http://blog.didispace.com/springbootproperties/

springboot快速入门(二)——项目属性配置(日志详解)的更多相关文章

  1. Spring Boot 2.x 快速入门(下)HelloWorld示例详解

    上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...

  2. SpringBoot 入门:项目属性配置

    开发一个SpringBoot 项目,首当其冲,必然是配置项目 一.项目属性配置 1. SpringBoot自带了Tomcat服务器,通过使用项目配置文件来修改项目的配置,如图配置了部署在80端口,目录 ...

  3. spring boot快速入门 2 :属性配置

    属性配置:在application.properties中配置 第一步:配置端口号和项目名 并启动 第二步:在浏览器查看请求 第二种配置方式: 在application.yml中配置.(较为常用) 注 ...

  4. elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  5. [02] SpringBoot的项目属性配置

    1.application.properties 简述 配置文件的使用和调整都非常方便,直接在项目默认的classpath下的application.properties文件中做调整即可.例如Spri ...

  6. SpringData 基于SpringBoot快速入门

    SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战 ...

  7. SpringBoot基础篇-SpringBoot快速入门

    SpringBoot基础 学习目标: 能够理解Spring的优缺点 能够理解SpringBoot的特点 能够理解SpringBoot的核心功能 能够搭建SpringBoot的环境 能够完成applic ...

  8. Springboot快速入门篇,图文并茂

    Springboot快速入门篇,图文并茂 文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star!搜索关注微信公众号 [码出Offer] 领取各种学习资料! image-20 ...

  9. SpringBoot快速入门(实战篇一)

    SpringBoot快速入门(一) 一SpringBoot简介 1.spring开发经历的阶段 Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 ...

随机推荐

  1. lua5.3+luasocket

    今天安装了luasocket,遇到了很多错误,百度并没有什么针对lua5.3的版本安装luasocket的文章,在这里记录一下. 1.下载lua5.3 下载到了lua-5.3.5.tar.gz版本的l ...

  2. clr_zmq Vs2010版本

    .net的消息队列很方便的一个库. 在github上的主版本虽然也支持fw4.0,但是必须使用vs2012以上进行编译. 这样就依赖vcredist运行时. 因为win7 sp1以下版本,无法安装vc ...

  3. Windows server 安装

    运行管理员CMD --先切换到安装环境目录cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 --安装服务 InstallUtil.exe D:\绝对路劲 ...

  4. JAVA两种代理模式

    简单设计动态代理,基本模拟spring的动态代理方式. before afterReturning around afterException after这些通知方法都可以这块模拟出来 spring的 ...

  5. MySQL的前缀索引及Oracle的类似实现

    MySQL有一个很有意思的索引类型,叫做前缀索引,它可以给某个文本字段的前面部分单独做索引,从而降低索引的大小. 其实,Oracle也有类似的实现,对于文本,它可以通过substr的函数索引,实现同样 ...

  6. PowerShell管理SCOM_批量设置维护模式(下)

    #定义存储需要置为维护模式的计算机名称列表$serverlist = "C:\scomm\servers.txt"#定义RMS服务器名称$rmsServerName=”scomw. ...

  7. 解决Failed to load the JNI shared library xxx/xxx/jvm.dll 错误

    原因:jdk发生变化(新装了32位jdk),eclipse在启动时使用了 系统环境变量中的jdk路径(32位). 解决:只要把旧的64位的jre路径指定给eclipse启动文件即可. 在eclipse ...

  8. TreeMap:是基于红黑树的Map接口的实现

    > TreeMap:是基于红黑树的Map接口的实现. 红黑树:平衡二叉树 取出时,可以有三种方式:前序遍历,中序遍历,后序遍历 >排序: A 自然排序  --TreeMap无参构造 Tre ...

  9. iptables简单规则记录

    先来一句:好记性不如烂笔头! 1.iptables简介 iptables是基于包过滤的防火墙,它主要工作在osi模型的2,,4层,也可以工作在7层(iptables + squid) 2.原理 防火墙 ...

  10. 硬盘上的一些算法小题目||and今天看了下林锐的书以及gdb调试 及一些变成算法小题目

    gdb调试:观察点,断点,事件捕捉点.step 进入函数,next 跳过函数,until 跳出循环,finish 结束函数 林锐:书后试题 & c++的对象模型图 看了二叉树的非递归遍历, 链 ...