日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot学习笔记(四)

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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里面默認可以不寫引號,“”(雙引號)會將其中的轉義符轉義,其他不轉義

xml是一個標記文檔:

<server>

? ? ? ?<port>8080</port>??

? ? ? ?<path>a\b\c</path>

</server>

2.yml的使用

①創建一個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容器內 @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 + "]";}}

?②創建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} 行內寫法#數組類型:habbies: [籃球,兵乓球,書法] #行內寫法#- 籃球#- 兵乓球#- 書法#集合類型:skills: [計算機,編程,springboot] #行內寫法#- 計算機#- 編程#- 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注解將配置文件的值映射到類上使用

總結

以上是生活随笔為你收集整理的springboot学习笔记(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。