当前位置:   article > 正文

SpringBoot基础 第一天

SpringBoot基础 第一天

SpringBoot配置的文件名是固定的:application.yml  application.properties

YAML:以数据为中心 比Json  xml更适合做配置文件

YAML语法:

1 字面量:普通值(字符串 布尔值 数字)

        (1) k: v

        (2) " "不会转义  ' '会转义

2 对象,map(属性和值)

        (1) k: v  

        (2) 在下一行 写对象的属性和值

    数组(List,set)

        用 - 表示数组中的一个元素

      行内写法

 

properties配置文件容易乱码 需要在设置中找到这个然后调成这样

 示例1(在yml文件中配置):

application.yaml文件中配置

  1. Person:
  2. name: 张三
  3. age: 21
  4. boss: true
  5. birth: 2022/12/5
  6. maps: {key1: value1,key2: value2}
  7. list: [dog,cat,pig]

Person类中利用ConfigurationProperties(prefix=" ")引入配置类中的元素 但是注意Person类中的类的名字必须与application.yaml中一致

  1. package com.qcby.model;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. @Component
  8. @ConfigurationProperties(prefix = "person")
  9. public class Person {
  10. private String name;
  11. private Integer age;
  12. private boolean boss;
  13. private Date birth;
  14. private Map<String,String> maps;
  15. private List<String> list;
  16. public Person() {
  17. }
  18. public Person(String name, Integer age, boolean boss, Date birth, Map<String, String> maps, List<String> list) {
  19. this.name = name;
  20. this.age = age;
  21. this.boss = boss;
  22. this.birth = birth;
  23. this.maps = maps;
  24. this.list = list;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public Integer getAge() {
  33. return age;
  34. }
  35. public void setAge(Integer age) {
  36. this.age = age;
  37. }
  38. public boolean isBoss() {
  39. return boss;
  40. }
  41. public void setBoss(boolean boss) {
  42. this.boss = boss;
  43. }
  44. public Date getBirth() {
  45. return birth;
  46. }
  47. public void setBirth(Date birth) {
  48. this.birth = birth;
  49. }
  50. public Map<String, String> getMaps() {
  51. return maps;
  52. }
  53. public void setMaps(Map<String, String> maps) {
  54. this.maps = maps;
  55. }
  56. public List<String> getList() {
  57. return list;
  58. }
  59. public void setList(List<String> list) {
  60. this.list = list;
  61. }
  62. @Override
  63. public String toString() {
  64. return "Person{" +
  65. "name='" + name + '\'' +
  66. ", age=" + age +
  67. ", boss=" + boss +
  68. ", birth=" + birth +
  69. ", maps=" + maps +
  70. ", list=" + list +
  71. '}';
  72. }
  73. }

最终在Test包下进行测试

  1. @RunWith(SpringRunner.class) //声明为测试单元
  2. @SpringBootTest //读取配置
  3. public class SpringBoot01ApplicationTests {
  4. @Autowired
  5. Person person;
  6. @Test
  7. public void Context(){
  8. System.out.println(person);
  9. }
  10. }

示例2(在properties文件中配置)

在application.properties中

  1. person.email=lucky
  2. person.hello=junit
  3. person.lastName=jiangjiayi
  4. person.age=12
  5. person.boss=true
  6. person.list=dog,cat
  7. person.maps.key1=value1
  8. person.maps.key2=value2
  9. person.dog.name=${person.hello} //${} 可以理解为Vue中的{{}} 调用的是上面的person.hello的值
  10. person.dog.age=16

Person类

  1. package com.qcby.model;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import javax.validation.constraints.Email;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.Map;
  8. @Component
  9. @ConfigurationProperties(prefix = "person")
  10. public class Person {
  11. String email;
  12. String hello;
  13. String lastName;
  14. int age;
  15. boolean boss;
  16. List<String> list;
  17. Map<String,String>maps;
  18. Dog dog;
  19. public String getEmail() {
  20. return email;
  21. }
  22. public void setEmail(String email) {
  23. this.email = email;
  24. }
  25. public String getHello() {
  26. return hello;
  27. }
  28. public void setHello(String hello) {
  29. this.hello = hello;
  30. }
  31. public String getLastName() {
  32. return lastName;
  33. }
  34. public void setLastName(String lastName) {
  35. this.lastName = lastName;
  36. }
  37. public int getAge() {
  38. return age;
  39. }
  40. public void setAge(int age) {
  41. this.age = age;
  42. }
  43. public boolean isBoss() {
  44. return boss;
  45. }
  46. public void setBoss(boolean boss) {
  47. this.boss = boss;
  48. }
  49. public List<String> getList() {
  50. return list;
  51. }
  52. public void setList(List<String> list) {
  53. this.list = list;
  54. }
  55. public Map<String, String> getMaps() {
  56. return maps;
  57. }
  58. public void setMaps(Map<String, String> maps) {
  59. this.maps = maps;
  60. }
  61. public Dog getDog() {
  62. return dog;
  63. }
  64. public void setDog(Dog dog) {
  65. this.dog = dog;
  66. }
  67. @Override
  68. public String toString() {
  69. return "Person{" +
  70. "email='" + email + '\'' +
  71. ", hello='" + hello + '\'' +
  72. ", lastName='" + lastName + '\'' +
  73. ", age=" + age +
  74. ", boss=" + boss +
  75. ", list=" + list +
  76. ", maps=" + maps +
  77. ", dog=" + dog +
  78. '}';
  79. }
  80. }

Dog类

  1. package com.qcby.model;
  2. public class Dog {
  3. String name;
  4. int age;
  5. public Dog() {
  6. }
  7. public Dog(String name, int age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. @Override
  12. public String toString() {
  13. return "Dog{" +
  14. "name='" + name + '\'' +
  15. ", age=" + age +
  16. '}';
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. }

测试类

  1. @RunWith(SpringRunner.class) //声明为测试单元
  2. @SpringBootTest //读取配置
  3. public class SpringBoot01ApplicationTests {
  4. @Autowired
  5. Person person;
  6. @Test
  7. public void Context(){
  8. System.out.println(person);
  9. }
  10. }

   如果不是标准的application.properties 是其他的Person类中需要用PropertySource来添加配置文件的路径

@ImportSource

导入Spring的配置文件,让配置文件里面的内容生效;Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式
1、配置类@Configuration------>Spring配置文件
2、使用@Bean给容器中添加组件,将方法的返回值添加到容器中 容器中这个组件的默认ID就是方法名

 生成随机数:

  1. ${random.value}、${random.int}、${random.long}
  2. ${random.int(10)}、${random.int[1024,65536]}

多profile文件

        通过spring:

                        profiles:

                                active:    来决定运行谁

配置文件加载位置的优先级

–file:./config/
–file:./
–classpath:/config/
–classpath:/

优先级由高到底,高优先级的配置会覆盖低优先级的配置; 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/937413
推荐阅读
相关标签
  

闽ICP备14008679号