做網(wǎng)站空間多大網(wǎng)站優(yōu)化包括對什么優(yōu)化
SpringBoot配置的文件名是固定的:application.yml? application.properties
YAML:以數(shù)據(jù)為中心 比Json? xml更適合做配置文件
YAML語法:
1 字面量:普通值(字符串 布爾值 數(shù)字)
? ? ? ? (1) k: v
? ? ? ? (2) " "不會轉(zhuǎn)義? ' '會轉(zhuǎn)義
2 對象,map(屬性和值)
? ? ? ? (1) k: v??
? ? ? ? (2) 在下一行 寫對象的屬性和值
? ? 數(shù)組(List,set)
? ? ? ? 用 - 表示數(shù)組中的一個元素
? ? ? 行內(nèi)寫法
?
properties配置文件容易亂碼 需要在設(shè)置中找到這個然后調(diào)成這樣
?示例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中的{{}} 調(diào)用的是上面的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
導(dǎo)入Spring的配置文件,讓配置文件里面的內(nèi)容生效;Spring Boot里面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別;想讓Spring的配置文件生效,加載進來;@ImportResource標注在一個配置類上
SpringBoot推薦給容器中添加組件的方式;推薦使用全注解的方式
1、配置類@Configuration------>Spring配置文件
2、使用@Bean給容器中添加組件,將方法的返回值添加到容器中 容器中這個組件的默認ID就是方法名
?生成隨機數(shù):
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
多profile文件
? ? ? ? 通過spring:
? ? ? ? ? ? ? ? ? ? ? ? profiles:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? active:? ? 來決定運行誰
配置文件加載位置的優(yōu)先級
–file:./config/
–file:./
–classpath:/config/
–classpath:/
優(yōu)先級由高到底,高優(yōu)先級的配置會覆蓋低優(yōu)先級的配置;?