SpringBoot(YAML)
Spring Initializer快速创建Spring Boot项目

src/main/java — 保存java源代码
src/main/resources
application.properties — Spring Boot应用的配置文件
[static] — 需要自己手动创建【保存web应用程序所需的静态资源{hrml、css、js、img}】
[templates] — 需要自己手动创建【保存模板页面】
Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面;
可以使用模板引擎freemarker、thymeleaf;
依赖@SpringBootApplication注解中包含的@EnableAutoConfiguration,SpringBoot项目会创建出自动的默认配置数据,以保证SpringBoot项目在创建成功以后,没有进行任何编程操作的情况下就可以正常运行起来。
在不同环境下SpringBoot项目所使用的自动配置的默认数据值就需要随着环境的变化而被修改,我们在修改的时候不能修改源码,而且源码页无法修改,基于这个情况,SpringBoot项目对外提供了一个可以用来修改自动配置的默认数据值的文件,这个文件就是src/main/resources/application.properties文件。
application.properties文件SpringBoot的核心配置文件
作用:修改自动配置的默认数据值的文件
名称:application.properties / application.yml
application.properties /application.yml就是同一个配置文件,后缀名的不同,表示这个文件中内容的书写风格不同。
例如:配置数据库驱动名称
//application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
//application.yml
spring:datasource:driver-class-name: com.mysql.jdbc.Driver
YAML(YAML Ain’t Markup Language)
YAML A Markup Language:是一个标记语言
说起标记语言我们会想到html/xml,它们都是标记语言。
Html【超文本标记语言】---- 默认提供好了标记 例如:< table>< /table>
Xml【可扩展的标记语言】-- 自己手动创建标记 例如:< student>< /student>
例如
Xml:配置例子
<server>
<port>8081</port>
</server>
YAML:配置例子
server:
Port:8081
YAML isn’t Markup Language:不是一个标记语言;是因为它以数据为中心的
YAML语法格式:
1、基本语法
键:(空格)值:表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的。
例如:
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/testjdbc:template:query-timeout: 1000
server:port: 9090
2. 属性值写法
属性值—键值对的键所对应的值
在上面的例子中url/port都是属性,实际都是xxxxxProperties类的成员变量。“jdbc:mysql://127.0.0.1:3306/test 、9090”就是属性值。
由于yml文件的属性,实际都是xxxxxProperties类的成员变量,成员变量都是有固定的数据类型,所以属性的值不能随便写,得符合成员变量对应的数据类型。
(1) 普通的值(数字,字符串,布尔)
1.1数字–数字值
1.2布尔–true/false
1.3字符串
默认不用加上单引号或者双引号;
如果有””[双引号],字符串中的转义字符会执行【\n—换行】
例如:name: “zhangsan\nlisi”
结果:
zhangsan \n---换行
lisi
如果有’’[单引号],字符串中的转义字符不会执行,会作为一个字符直接使用【\n–\n】
例如:name: ‘zhangsan\nlisi’
结果:
zhangsan\nlisi
(2) 对象
2.1对象名称对象中的属性换行缩进表示
例如:
student: stuid: 1001stuname: zhangsanstuage: 23stuaddress: 西安
student----对象名称
stuid、stuname、stuage、stuaddress----对象中的属性
1001、zhangsan、23、西安----属性的数据值
2.2对象名称: {属性名称:属性值,属性名称:属性值}
例如:[行内写法]
person: {perid:1002,pername: lisi,perage: 24,peraddress: 北京}
(3)集合
数组类型的集合(List、Set)
1> ”-[空格]数组集合中的元素值”
例如:
javas: - javase- javaee- javame
javas----数组名称
javase、javaee、javame---数组集合中的数据值
2> 数组名称: [数据值1,数据值2]
例如:行内写法
names: [zhangsan,lisi,wangwu]
Map(键值对):
Map集合的名称: {key1:value1,key2:value2}
例如:maps: {name: zhangsan,age: 23,address: 西安}
3> 将YAML文件中的数据值绑定到javabean上
通过@ConfigurationProperties将核心配置文件中的数据值与javabean类中的成员变量绑定
@ConfigurationProperties有一个属性prefix
prefix = “YAML文件中的对象名称”
具体步骤:
1. 导入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
2. 创建javabean
3. Javabean上添加注解
@Component
@ConfigurationProperties(prefix = "student")
4. 在resources的application.yml配置属性
student:stuid: 1001stuname: zhangsanstusex: truelikes:- game- book- playmaps: {xiaoxue: 长安县,zhongxue: 雁塔区}my-address: {addressid: 1,addressname: home,addressinfo: 长安一路}
在resources的application.properties配置属性
student.stuid=1002
student.stuname=lisi
student.stusex=true
student.likes=game,book,play
#student.likes[0]=game
#student.likes[1]=book
#student.likes[2]=play
student.maps.xiaoxue=长安县
student.maps.zhongxue=雁塔区
student.my-address.addressid=1
student.my-address.addressname=home
student.my-address.addressinfo=长安一路
由于properties配置文件在idea中默认utf-8可能会有中文乱码,所以需要设置修改转码;

测试用来的控制器类
package com.wangxing.springboot.controller;
import com.wangxing.springboot.bean.MyAddress;
import com.wangxing.springboot.bean.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;
import java.util.Map;@Controller
public class StudentController {@Autowiredprivate StudentBean studentBean;@RequestMapping(value = "/getStuInfo")@ResponseBodypublic String getStuInfo(){System.out.println("student=="+studentBean);int stuid=studentBean.getStuid();String stuname=studentBean.getStuname();String stuinfo=stuid+" "+stuname;List<String> likes=studentBean.getLikes();for(String like:likes){System.out.println("like=="+like);}Map<String,String> maps=studentBean.getMaps();for(Map.Entry<String,String> entry:maps.entrySet()){System.out.println(entry.getKey()+" "+entry.getValue());}MyAddress myAddress= studentBean.getMyAddress();System.out.println(myAddress.getAddressid()+" "+myAddress.getAddressname());return stuinfo;}
}
通过@Value将数据值与javabean类中的成员变量绑定
package com.wangxing.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PersonBean {@Value("1001")private int perid;@Value("zhangsan")private String pername;public int getPerid() {return perid;}public void setPerid(int perid) {this.perid = perid;}public String getPername() {return pername;}public void setPername(String pername) {this.pername = pername;}
}
测试代码
package com.wangxing.springboot.controller;
import com.wangxing.springboot.bean.PersonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class PersonController {@Autowiredprivate PersonBean personBean;@RequestMapping(value = "/getPersonInfo")@ResponseBodypublic String getPersonInfo(){int id=personBean.getPerid();String name=personBean.getPername();System.out.println(id+" "+name);return "hello";}
}
@Value获取值和@ConfigurationProperties获取值比较

配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
