当前位置:   article > 正文

SpringBoot_3

SpringBoot_3

SpringBoot_3

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就是同一个配置文件,后缀名的不同,表示这个文件中内容的书写风格不同。

例如:配置数据库驱动名称

  1. application.properties
  2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. application.yml
  4. spring:
  5. datasource:
  6. 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:配置例子

  1. <server>
  2. <port>8081</port>
  3. </server>

YAML:配置例子

  1. server:
  2. Port:8081

YAML isn't Markup Language:不是一个标记语言;是因为它以数据为中心的

 

YAML语法格式:

1、基本语法

键:(空格)值:表示一对键值对(空格必须有);

以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的。

例如:

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://127.0.0.1:3306/test
  5. jdbc:
  6. template:
  7. query-timeout: 1000
  8. server:
  9. 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对象名称对象中的属性换行缩进表示

例如:

  1. student:
  2.                stuid: 1001
  3.                stuname: zhangsan
  4.                stuage: 23
  5.                stuaddress: 西安

student----对象名称

stuid、stuname、stuage、stuaddress----对象中的属性

  1. zhangsan、23、西安----属性的数据值

2.2对象名称: {属性名称:属性值,属性名称:属性值}

例如:[行内写法]

                          person-bean: {"perid":1234,"pername":"person"}

3.集合

数组类型的集合(List、Set)

  1. ”-[空格]数组集合中的元素值”

例如:

javas:
               - javase
               - javaee
                - javame

javas----数组名称

javase、javaee、javame---数组集合中的数据值

  1. 数组名称: [数据值1,数据值2]

例如:行内写法

Na   

Map(键值对):

Map集合的名称: {key1:value1,key2:value2}

例如:maps: {name: zhangsan,age: 23,address: 西安}

3.将YAML文件中的数据值绑定到javabean上

通过@ConfigurationProperties将核心配置文件中的数据值与javabean类中的成员变量绑定

@ConfigurationProperties有一个属性prefix

prefix = "YAML文件中的对象名称"

具体步骤:

  1. 导入依赖
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-configuration-processor</artifactId>
  4. <optional>true</optional>
  5. </dependency>

2.创建javabean

  1. package com.wanxging.springboot.springbootdemo3.bean;
  2. public class PersonBean {
  3. private int perid;
  4. private String pername;
  5. public int getPerid() {
  6. return perid;
  7. }
  8. public void setPerid(int perid) {
  9. this.perid = perid;
  10. }
  11. public String getPername() {
  12. return pername;
  13. }
  14. public void setPername(String pername) {
  15. this.pername = pername;
  16. }
  17. }
  1. package com.wanxging.springboot.springbootdemo3.bean;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. @Component
  8. @ConfigurationProperties(prefix = "student")
  9. public class StudentBean {
  10. private int stuint;
  11. private double studouble;
  12. private boolean stuboolean;
  13. private String stustring;
  14. private PersonBean personBean;
  15. private List<String> stulist;
  16. private Set<String> stuset;
  17. private Map<String,String> stumap;
  18. public int getStuint() {
  19. return stuint;
  20. }
  21. public void setStuint(int stuint) {
  22. this.stuint = stuint;
  23. }
  24. public double getStudouble() {
  25. return studouble;
  26. }
  27. public void setStudouble(double studouble) {
  28. this.studouble = studouble;
  29. }
  30. public boolean isStuboolean() {
  31. return stuboolean;
  32. }
  33. public void setStuboolean(boolean stuboolean) {
  34. this.stuboolean = stuboolean;
  35. }
  36. public String getStustring() {
  37. return stustring;
  38. }
  39. public void setStustring(String stustring) {
  40. this.stustring = stustring;
  41. }
  42. public PersonBean getPersonBean() {
  43. return personBean;
  44. }
  45. public void setPersonBean(PersonBean personBean) {
  46. this.personBean = personBean;
  47. }
  48. public List<String> getStulist() {
  49. return stulist;
  50. }
  51. public void setStulist(List<String> stulist) {
  52. this.stulist = stulist;
  53. }
  54. public Set<String> getStuset() {
  55. return stuset;
  56. }
  57. public void setStuset(Set<String> stuset) {
  58. this.stuset = stuset;
  59. }
  60. public Map<String, String> getStumap() {
  61. return stumap;
  62. }
  63. public void setStumap(Map<String, String> stumap) {
  64. this.stumap = stumap;
  65. }
  66. }

3.Javabean上添加注解

@Component
@ConfigurationProperties(prefix = "student")

4.在resources的application.yml配置属性

  1. student:
  2. stuint: 1001
  3. studouble: 12.5
  4. stuboolean: false
  5. stustring: 'zhangsan\nlisi'
  6. stulist:
  7. - zhangsan
  8. - lisi
  9. - wangwu
  10. stuset: [javase,javaee,javame]
  11. stumap: {"name":zhangsan,"age":23,"address":"西安"}
  12. #person-bean:
  13. #perid: 1234
  14. #pername: "person"
  15. person-bean: {"perid":1234,"pername":"person"}

导入web依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

测试用来的控制器类

  1. package com.wanxging.springboot.springbootdemo3.controller;
  2. import com.wanxging.springboot.springbootdemo3.bean.PersonBean;
  3. import com.wanxging.springboot.springbootdemo3.bean.StudentBean;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.Map;
  8. @RestController
  9. @RequestMapping("/test")
  10. public class TestController {
  11. @Autowired
  12. private StudentBean studentBean;
  13. @RequestMapping("/testBeanValue.do")
  14. public void testBeanValue(){
  15. int stuint=studentBean.getStuint();
  16. double studouble=studentBean.getStudouble();
  17. boolean stuboolean=studentBean.isStuboolean();
  18. String stustring=studentBean.getStustring();
  19. System.out.println("stuint=="+stuint);
  20. System.out.println("studouble=="+studouble);
  21. System.out.println("stuboolean=="+stuboolean);
  22. System.out.println("stustring=="+stustring);
  23. System.out.println("------------------");
  24. for(String listvalue:studentBean.getStulist()){
  25. System.out.println("stulist=="+listvalue);
  26. }
  27. System.out.println("------------------");
  28. for(String setvalue:studentBean.getStuset()){
  29. System.out.println("stuset=="+setvalue);
  30. }
  31. System.out.println("------------------");
  32. for(Map.Entry<String,String> entry:studentBean.getStumap().entrySet()){
  33. System.out.println(entry.getKey()+"=="+entry.getValue());
  34. }
  35. System.out.println("------------------");
  36. PersonBean personBean=studentBean.getPersonBean();
  37. System.out.println("perid=="+personBean.getPerid());
  38. System.out.println("pername=="+personBean.getPername());
  39. }
  40. }

上面的例子中处理的是application.yml格式的写法与javabean的绑定

application.properties格式的写法与javabean的绑定

在resources的application.properties配置属性

  1. student.stuint=1001
  2. student.studouble=12.5
  3. student.stuboolean=false
  4. student.stustring='zhangsan\\nlisi'
  5. #student.stulist=zhangsan,lisi,wangwu
  6. student.stulist[0]=zhangsan
  7. student.stulist[1]=lisi
  8. student.stulist[2]=wangwu
  9. #student.stuset=javase,javaee,javame
  10. student.stuset[0]=javase
  11. student.stuset[1]=javaee
  12. student.stuset[2]=javame
  13. student.stumap.name="zhangsan"
  14. student.stumap.age=23
  15. student.stumap.address="xian"
  16. student.person-bean.perid=1234
  17. student.person-bean.pername="person"

由于properties配置文件在idea中默认utf-8可能会有中文乱码,所以需要设置修改转码;

 

通过@Value将数据值与javabean类中的成员变量绑定

  1. package com.wangxing.springboot.springbootdemo5.bean;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. @Component
  8. public class StudentBean {
  9. @Value("1001")
  10. private int stuint;
  11. @Value("12.5")
  12. private double studouble;
  13. @Value("false")
  14. private boolean stuboolean;
  15. @Value("zhangsan")
  16. private String stustring;
  17. //@Value("perid:1001")
  18. private PersonBean personBean;
  19. @Value("zhangsan,lisi,wangwu")
  20. private List<String> stulist;
  21. @Value("zhangsan,lisi,wangwu")
  22. private Set<String> stuset;
  23. //@Value("'name':'zhangsan','age':23,'address':'xian'")
  24. private Map<String,String> stumap;
  25. public int getStuint() {
  26. return stuint;
  27. }
  28. public void setStuint(int stuint) {
  29. this.stuint = stuint;
  30. }
  31. public double getStudouble() {
  32. return studouble;
  33. }
  34. public void setStudouble(double studouble) {
  35. this.studouble = studouble;
  36. }
  37. public boolean isStuboolean() {
  38. return stuboolean;
  39. }
  40. public void setStuboolean(boolean stuboolean) {
  41. this.stuboolean = stuboolean;
  42. }
  43. public String getStustring() {
  44. return stustring;
  45. }
  46. public void setStustring(String stustring) {
  47. this.stustring = stustring;
  48. }
  49. public PersonBean getPersonBean() {
  50. return personBean;
  51. }
  52. public void setPersonBean(PersonBean personBean) {
  53. this.personBean = personBean;
  54. }
  55. public List<String> getStulist() {
  56. return stulist;
  57. }
  58. public void setStulist(List<String> stulist) {
  59. this.stulist = stulist;
  60. }
  61. public Set<String> getStuset() {
  62. return stuset;
  63. }
  64. public void setStuset(Set<String> stuset) {
  65. this.stuset = stuset;
  66. }
  67. public Map<String, String> getStumap() {
  68. return stumap;
  69. }
  70. public void setStumap(Map<String, String> stumap) {
  71. this.stumap = stumap;
  72. }
  73. }

 

测试代码

  1. package com.wangxing.springboot.springbootdemo5.controller;
  2. import com.wangxing.springboot.springbootdemo5.bean.PersonBean;
  3. import com.wangxing.springboot.springbootdemo5.bean.StudentBean;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.Map;
  8. @RestController
  9. @RequestMapping("/test")
  10. public class TestController {
  11. @Autowired
  12. private StudentBean studentBean;
  13. @RequestMapping("/testBeanValue.do")
  14. public void testBeanValue(){
  15. int stuint=studentBean.getStuint();
  16. double studouble=studentBean.getStudouble();
  17. boolean stuboolean=studentBean.isStuboolean();
  18. String stustring=studentBean.getStustring();
  19. System.out.println("stuint=="+stuint);
  20. System.out.println("studouble=="+studouble);
  21. System.out.println("stuboolean=="+stuboolean);
  22. System.out.println("stustring=="+stustring);
  23. System.out.println("------------------");
  24. for(String listvalue:studentBean.getStulist()){
  25. System.out.println("stulist=="+listvalue);
  26. }
  27. System.out.println("------------------");
  28. for(String setvalue:studentBean.getStuset()){
  29. System.out.println("stuset=="+setvalue);
  30. }
  31. /*
  32. System.out.println("------------------");
  33. for(Map.Entry<String,String> entry:studentBean.getStumap().entrySet()){
  34. System.out.println(entry.getKey()+"=="+entry.getValue());
  35. }
  36. System.out.println("------------------");
  37. PersonBean personBean=studentBean.getPersonBean();
  38. System.out.println("perid=="+personBean.getPerid());
  39. System.out.println("pername=="+personBean.getPername());
  40. */
  41. }
  42. }

 

person.properties

  1. myid=1234
  2. myname=zhansgan

PersonBean.java

  1. package com.wangxing.springboot.springbootdemo5.bean;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.PropertySource;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @PropertySource({"classpath:application.properties","classpath:person.properties"})
  7. public class PersonBean {
  8. @Value("${myid}")
  9. private int perid;
  10. @Value("${myname}")
  11. private String pername;
  12. public int getPerid() {
  13. return perid;
  14. }
  15. public void setPerid(int perid) {
  16. this.perid = perid;
  17. }
  18. public String getPername() {
  19. return pername;
  20. }
  21. public void setPername(String pername) {
  22. this.pername = pername;
  23. }
  24. }

@Value获取值和@ConfifigurationProperties获取值比较

配置文件yml还是properties他们都能获取到值;

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfifigurationProperties

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/673959
推荐阅读
相关标签
  

闽ICP备14008679号