springboot学习笔记(四)
springboot配置文件及yml的使用
1.配置文件
作用:springboot自動配置是基于約定的,可以使用配置文件對默認的配置或約定進行修改
默認的全局配置文件:
①application.properties :
寫法:k=v
示例:
server.port = 8880
②application.yml :yml不是一個標記文檔
寫法:k:空格v
示例:
server:
? ? ? ?port: 8880
? ? ? ?path: a\b\c
yml里面默認可以不寫引號,“”(雙引號)會將其中的轉(zhuǎn)義符轉(zhuǎn)義,其他不轉(zhuǎn)義
xml是一個標記文檔:
<server>
? ? ? ?<port>8080</port>??
? ? ? ?<path>a\b\c</path>
</server>
2.yml的使用
①創(chuàng)建一個student類(Student.class)
package com.example.bean;import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component //將javabean放入spring容器內(nèi) @ConfigurationProperties(prefix = "student")//spring-boot 提供@ConfigurationProperties注解將配置文件的值映射到類上使用 public class Student {private String name;private int age;private boolean sex;private Date birthday;private Map<String,Object> location;private String[] habbies;private List<String> skills;private Pet pet;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;}public boolean isSex() {return sex;}public void setSex(boolean sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Map<String, Object> getLocation() {return location;}public void setLocation(Map<String, Object> location) {this.location = location;}public String[] getHabbies() {return habbies;}public void setHabbies(String[] habbies) {this.habbies = habbies;}public List<String> getSkills() {return skills;}public void setSkills(List<String> skills) {this.skills = skills;}public Pet getPet() {return pet;}public void setPet(Pet pet) {this.pet = pet;}public Student(String name, int age, boolean sex, Date birthday, Map<String, Object> location, String[] habbies,List<String> skills, Pet pet) {super();this.name = name;this.age = age;this.sex = sex;this.birthday = birthday;this.location = location;this.habbies = habbies;this.skills = skills;this.pet = pet;}public Student() {super();}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", habbies="+ Arrays.toString(habbies) + ", skills=" + skills + ", pet=" + pet + "]";}}?②創(chuàng)建Pet類(Pet.class)
package com.example.bean;public class Pet {private String nickname;private String strain;public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public String getStrain() {return strain;}public void setStrain(String strain) {this.strain = strain;}@Overridepublic String toString() {return "Pet [nickname=" + nickname + ", strain=" + strain + "]";}public Pet(String nickname, String strain) {super();this.nickname = nickname;this.strain = strain;}public Pet() {super();}}?③編寫yml文件(application.yml)
student: #簡單類型name: djkage: 20sex: truebirthday: 2000/07/15#map類型:location: #寫法2:province: sdcity: wfzone: sg#寫法1:{province: sd,city: wf,zone: sg} 行內(nèi)寫法#數(shù)組類型:habbies: [籃球,兵乓球,書法] #行內(nèi)寫法#- 籃球#- 兵乓球#- 書法#集合類型:skills: [計算機,編程,springboot] #行內(nèi)寫法#- 計算機#- 編程#- springboot#類 類型Pet:#寫法2{nickname: xiaobai,strain: jiwawa} #寫法1#nickname: xiaobai#strain: jiwawa④測試?
package com.example.SpringbootDemo;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import com.example.bean.Student;@RunWith(SpringRunner.class) @SpringBootTest @EnableConfigurationProperties(Student.class)//3.通過@Autowired標簽即可訪問到該對象,不過在使用之前必須在使用類上面增加注解@EnableConfigurationProperties public class SpringbootDemoApplicationTests {@AutowiredStudent student;@Testpublic void contextLoads() {System.out.println(student);}}注解
1. @EnableConfigurationProperties(Student.class)
? ? ? ? 通過@Autowired標簽即可訪問到該對象,不過在使用之前必須在使用類上面增加注解@EnableConfigurationProperties?
2. @ConfigurationProperties(prefix = "student")
? ? ? ? ?spring-boot 提供@ConfigurationProperties注解將配置文件的值映射到類上使用
總結(jié)
以上是生活随笔為你收集整理的springboot学习笔记(四)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot学习笔记(三)
- 下一篇: 为什么商家数字化离不开交易平台