赞
踩
1.Spring Initializer快速创建Spring Boot项目
src/main/java----保存java源代码
src/main/resources
application.properties-------Spring Boot应用的配置文件
[static]---需要自己手动创建【保存web应用程序所需的静态资源{html、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.Driver
- url: jdbc:mysql://127.0.0.1:3306/test
- jdbc:
- template:
- query-timeout: 1000
- server:
- port: 9090
属性值---键值对的键所对应的值
在上面的例子中url/port都是属性,实际都是xxxxxProperties类的成员变量。“jdbc:mysql://127.0.0.1:3306/test 、9090”就是属性值。
由于yml文件的属性,实际都是xxxxxProperties类的成员变量,成员变量都是有固定的数据类型,所以属性的值不能随便写,得符合成员变量对应的数据类型。
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: 1001
- stuname: zhangsan
- stuage: 23
- stuaddress: 西安
student----对象名称
stuid、stuname、stuage、stuaddress----对象中的属性
2.2对象名称: {属性名称:属性值,属性名称:属性值}
例如:[行内写法]
person-bean: {"perid":1234,"pername":"person"}
3.集合
数组类型的集合(List、Set)
例如:
javas:
- javase
- javaee
- javame
javas----数组名称
javase、javaee、javame---数组集合中的数据值
例如:行内写法
Na
Map(键值对):
Map集合的名称: {key1:value1,key2:value2}
例如:maps: {name: zhangsan,age: 23,address: 西安}
3.将YAML文件中的数据值绑定到javabean上
通过@ConfigurationProperties将核心配置文件中的数据值与javabean类中的成员变量绑定
@ConfigurationProperties有一个属性prefix
prefix = "YAML文件中的对象名称"
具体步骤:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
2.创建javabean
- package com.wanxging.springboot.springbootdemo3.bean;
-
- public class PersonBean {
- private int perid;
- 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.wanxging.springboot.springbootdemo3.bean;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- @Component
- @ConfigurationProperties(prefix = "student")
- public class StudentBean {
- private int stuint;
- private double studouble;
- private boolean stuboolean;
- private String stustring;
- private PersonBean personBean;
- private List<String> stulist;
- private Set<String> stuset;
- private Map<String,String> stumap;
-
- public int getStuint() {
- return stuint;
- }
-
- public void setStuint(int stuint) {
- this.stuint = stuint;
- }
-
- public double getStudouble() {
- return studouble;
- }
-
- public void setStudouble(double studouble) {
- this.studouble = studouble;
- }
-
- public boolean isStuboolean() {
- return stuboolean;
- }
-
- public void setStuboolean(boolean stuboolean) {
- this.stuboolean = stuboolean;
- }
-
- public String getStustring() {
- return stustring;
- }
-
- public void setStustring(String stustring) {
- this.stustring = stustring;
- }
-
- public PersonBean getPersonBean() {
- return personBean;
- }
-
- public void setPersonBean(PersonBean personBean) {
- this.personBean = personBean;
- }
-
- public List<String> getStulist() {
- return stulist;
- }
-
- public void setStulist(List<String> stulist) {
- this.stulist = stulist;
- }
-
- public Set<String> getStuset() {
- return stuset;
- }
-
- public void setStuset(Set<String> stuset) {
- this.stuset = stuset;
- }
-
- public Map<String, String> getStumap() {
- return stumap;
- }
-
- public void setStumap(Map<String, String> stumap) {
- this.stumap = stumap;
- }
- }
3.Javabean上添加注解
@Component
@ConfigurationProperties(prefix = "student")
4.在resources的application.yml配置属性
- student:
- stuint: 1001
- studouble: 12.5
- stuboolean: false
- stustring: 'zhangsan\nlisi'
- stulist:
- - zhangsan
- - lisi
- - wangwu
- stuset: [javase,javaee,javame]
- stumap: {"name":zhangsan,"age":23,"address":"西安"}
- #person-bean:
- #perid: 1234
- #pername: "person"
- person-bean: {"perid":1234,"pername":"person"}
导入web依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
测试用来的控制器类
- package com.wanxging.springboot.springbootdemo3.controller;
-
- import com.wanxging.springboot.springbootdemo3.bean.PersonBean;
- import com.wanxging.springboot.springbootdemo3.bean.StudentBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.Map;
-
- @RestController
- @RequestMapping("/test")
- public class TestController {
- @Autowired
- private StudentBean studentBean;
- @RequestMapping("/testBeanValue.do")
- public void testBeanValue(){
- int stuint=studentBean.getStuint();
- double studouble=studentBean.getStudouble();
- boolean stuboolean=studentBean.isStuboolean();
- String stustring=studentBean.getStustring();
- System.out.println("stuint=="+stuint);
- System.out.println("studouble=="+studouble);
- System.out.println("stuboolean=="+stuboolean);
- System.out.println("stustring=="+stustring);
- System.out.println("------------------");
- for(String listvalue:studentBean.getStulist()){
- System.out.println("stulist=="+listvalue);
- }
- System.out.println("------------------");
- for(String setvalue:studentBean.getStuset()){
- System.out.println("stuset=="+setvalue);
- }
- System.out.println("------------------");
- for(Map.Entry<String,String> entry:studentBean.getStumap().entrySet()){
- System.out.println(entry.getKey()+"=="+entry.getValue());
- }
- System.out.println("------------------");
- PersonBean personBean=studentBean.getPersonBean();
- System.out.println("perid=="+personBean.getPerid());
- System.out.println("pername=="+personBean.getPername());
- }
- }
上面的例子中处理的是application.yml格式的写法与javabean的绑定
application.properties格式的写法与javabean的绑定
在resources的application.properties配置属性
- student.stuint=1001
- student.studouble=12.5
- student.stuboolean=false
- student.stustring='zhangsan\\nlisi'
- #student.stulist=zhangsan,lisi,wangwu
- student.stulist[0]=zhangsan
- student.stulist[1]=lisi
- student.stulist[2]=wangwu
- #student.stuset=javase,javaee,javame
- student.stuset[0]=javase
- student.stuset[1]=javaee
- student.stuset[2]=javame
- student.stumap.name="zhangsan"
- student.stumap.age=23
- student.stumap.address="xian"
- student.person-bean.perid=1234
- student.person-bean.pername="person"
由于properties配置文件在idea中默认utf-8可能会有中文乱码,所以需要设置修改转码;
通过@Value将数据值与javabean类中的成员变量绑定
- package com.wangxing.springboot.springbootdemo5.bean;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- @Component
- public class StudentBean {
- @Value("1001")
- private int stuint;
- @Value("12.5")
- private double studouble;
- @Value("false")
- private boolean stuboolean;
- @Value("zhangsan")
- private String stustring;
- //@Value("perid:1001")
- private PersonBean personBean;
- @Value("zhangsan,lisi,wangwu")
- private List<String> stulist;
- @Value("zhangsan,lisi,wangwu")
- private Set<String> stuset;
- //@Value("'name':'zhangsan','age':23,'address':'xian'")
- private Map<String,String> stumap;
-
- public int getStuint() {
- return stuint;
- }
-
- public void setStuint(int stuint) {
- this.stuint = stuint;
- }
-
- public double getStudouble() {
- return studouble;
- }
-
- public void setStudouble(double studouble) {
- this.studouble = studouble;
- }
-
- public boolean isStuboolean() {
- return stuboolean;
- }
-
- public void setStuboolean(boolean stuboolean) {
- this.stuboolean = stuboolean;
- }
-
- public String getStustring() {
- return stustring;
- }
-
- public void setStustring(String stustring) {
- this.stustring = stustring;
- }
-
- public PersonBean getPersonBean() {
- return personBean;
- }
-
- public void setPersonBean(PersonBean personBean) {
- this.personBean = personBean;
- }
-
- public List<String> getStulist() {
- return stulist;
- }
-
- public void setStulist(List<String> stulist) {
- this.stulist = stulist;
- }
-
- public Set<String> getStuset() {
- return stuset;
- }
-
- public void setStuset(Set<String> stuset) {
- this.stuset = stuset;
- }
-
- public Map<String, String> getStumap() {
- return stumap;
- }
-
- public void setStumap(Map<String, String> stumap) {
- this.stumap = stumap;
- }
- }
测试代码
- package com.wangxing.springboot.springbootdemo5.controller;
-
- import com.wangxing.springboot.springbootdemo5.bean.PersonBean;
- import com.wangxing.springboot.springbootdemo5.bean.StudentBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.Map;
-
- @RestController
- @RequestMapping("/test")
- public class TestController {
- @Autowired
- private StudentBean studentBean;
- @RequestMapping("/testBeanValue.do")
- public void testBeanValue(){
- int stuint=studentBean.getStuint();
- double studouble=studentBean.getStudouble();
- boolean stuboolean=studentBean.isStuboolean();
- String stustring=studentBean.getStustring();
- System.out.println("stuint=="+stuint);
- System.out.println("studouble=="+studouble);
- System.out.println("stuboolean=="+stuboolean);
- System.out.println("stustring=="+stustring);
-
- System.out.println("------------------");
- for(String listvalue:studentBean.getStulist()){
- System.out.println("stulist=="+listvalue);
- }
-
- System.out.println("------------------");
- for(String setvalue:studentBean.getStuset()){
- System.out.println("stuset=="+setvalue);
- }
- /*
- System.out.println("------------------");
- for(Map.Entry<String,String> entry:studentBean.getStumap().entrySet()){
- System.out.println(entry.getKey()+"=="+entry.getValue());
- }
- System.out.println("------------------");
- PersonBean personBean=studentBean.getPersonBean();
- System.out.println("perid=="+personBean.getPerid());
- System.out.println("pername=="+personBean.getPername());
- */
- }
- }
person.properties
- myid=1234
- myname=zhansgan
PersonBean.java
- package com.wangxing.springboot.springbootdemo5.bean;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.stereotype.Component;
-
- @Component
- @PropertySource({"classpath:application.properties","classpath:person.properties"})
- public class PersonBean {
- @Value("${myid}")
- private int perid;
- @Value("${myname}")
- 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;
- }
- }
@Value获取值和@ConfifigurationProperties获取值比较
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfifigurationProperties;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。