当前位置: 首页 > news >正文

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文件中配置

Person:name: 张三age: 21boss: truebirth: 2022/12/5maps: {key1: value1,key2: value2}list: [dog,cat,pig]

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

package com.qcby.model;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String name;private Integer age;private boolean boss;private Date birth;private Map<String,String> maps;private List<String> list;public Person() {}public Person(String name, Integer age, boolean boss, Date birth, Map<String, String> maps, List<String> list) {this.name = name;this.age = age;this.boss = boss;this.birth = birth;this.maps = maps;this.list = list;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public boolean isBoss() {return boss;}public void setBoss(boolean boss) {this.boss = boss;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, String> getMaps() {return maps;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", boss=" + boss +", birth=" + birth +", maps=" + maps +", list=" + list +'}';}
}

最终在Test包下进行测试

@RunWith(SpringRunner.class)  //声明为测试单元
@SpringBootTest  //读取配置
public class SpringBoot01ApplicationTests {@AutowiredPerson person;@Testpublic void Context(){System.out.println(person);}}

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

在application.properties中

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

Person类

package com.qcby.model;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;@Component
@ConfigurationProperties(prefix = "person")
public class Person {String email;String hello;String lastName;int age;boolean boss;List<String> list;Map<String,String>maps;Dog dog;public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getHello() {return hello;}public void setHello(String hello) {this.hello = hello;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isBoss() {return boss;}public void setBoss(boolean boss) {this.boss = boss;}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}public Map<String, String> getMaps() {return maps;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}@Overridepublic String toString() {return "Person{" +"email='" + email + '\'' +", hello='" + hello + '\'' +", lastName='" + lastName + '\'' +", age=" + age +", boss=" + boss +", list=" + list +", maps=" + maps +", dog=" + dog +'}';}
}

Dog类

package com.qcby.model;public class Dog {String name;int age;public Dog() {}public Dog(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

测试类

@RunWith(SpringRunner.class)  //声明为测试单元
@SpringBootTest  //读取配置
public class SpringBoot01ApplicationTests {@AutowiredPerson person;@Testpublic void Context(){System.out.println(person);}}

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

@ImportSource

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

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

 生成随机数:

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

多profile文件

        通过spring:

                        profiles:

                                active:    来决定运行谁

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

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

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

http://www.lryc.cn/news/415944.html

相关文章:

  • 【C/C++】C语言和C++实现Stack(栈)对比
  • mysql定时备份脚本
  • 云原生 (1)
  • gitlab-pages创建静态站点
  • Python爬虫技术 案例集锦
  • 实战OpenCV之环境安装与配置
  • Android应用开发面试之Jetpack面试题分析汇总
  • 【数据结构】栈的概念、结构和实现详解
  • LeetCode 每日一题 2024/7/29-2024/8/4
  • Golang死锁vs操作系统死锁
  • c/c++中π怎么定义
  • 基于whisper流式语音识别
  • Web3 市场暴跌的时候,哪些token跌的少,哪些还涨了? binance 数据爬取及分析
  • ffmpeg获得视频的音频文件
  • Robot Operating System——深度解析单线程执行器(SingleThreadedExecutor)执行逻辑
  • 【TS】使用npm全局安装typescript
  • 安全用户角色权限
  • 代理模式学习
  • 深入理解Go 语言信号量 Semaphore
  • VisualStudio2019下载与安装
  • 李宏毅老师机器学习常见英语词汇
  • 人工智能时代,程序员如何保持核心竞争力?
  • WiFi to Ethernet: 树莓派共享无线连接至有线网口,自动通过Captive Poartal网页登录认证
  • 【神软大数据治理平台-高级动态SQL(接口开发)】
  • 【Java数据结构】Map和Set超详细两万字讲解(内含搜索树+哈希表)
  • 中国制造2025,会抛弃精益生产吗?
  • Rust 循环
  • 数据结构(其四)--特殊矩阵的存储
  • 系统化学习 H264视频编码(06)哥伦布编码
  • 手机在网状态接口如何对接?(一)