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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot配置文件与配置类的属性映射方式

發布時間:2023/12/3 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot配置文件与配置类的属性映射方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、在pom文件中加入依賴

目錄結構


在實體類中會出現錯誤,然后點擊這個網址會有需要的依賴
網址:
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor
引入的依賴為:

<dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-configuration-processor </artifactId><optional> true </optional></dependency> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--繼承父工程--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.william</groupId><artifactId>day01_springboot_initializr</artifactId><version>0.0.1-SNAPSHOT</version><name>day01_springboot_initializr</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-configuration-processor </artifactId><optional> true </optional></dependency><!--spring boot開發工具包--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

二、application.yml

#配置對象 person:name: zhangsanage: 19date: 2019/09/09# 數組配置city:- beijing- tianjin- shanghai- chongqing# 配置集合 student: - name: zhangsanage: 18score: 100 - name: lisiage: 28score: 88 - name: wangwuage: 38score: 90 #注意事項:在key與value直接需要加入空格,對大小寫敏感。 server:port: 8081servlet:context-path: /demo

三、application.properties

server.port=8081 server.servlet.context-path=/demo

四、Person

需要提供get/set方法 和toString方法

還需要注解@Component

需要注解@ConfigurationProperties(prefix = “person”)

使用注解@Con?gurationProperties映射

通過注解@Con?gurationProperties(pre?x=’'配置文件中的key的前綴")可以將配置文件中的配置自動與實體進行映 射。
使用@Con?gurationProperties方式必須提供Setter方法,使用@Value注解不需要Setter方法

package com.william.day01_springboot_initializr.domain;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;import java.util.Arrays; import java.util.Date;/*** @author :lijunxuan* @date :Created in 2019/6/27 19:29* @description :* @version: 1.0*/ @Component @ConfigurationProperties(prefix = "person") public class Person {private String name;private String age;private Date date;private String [] city;@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age='" + age + '\'' +", date=" + date +", city=" + Arrays.toString(city) +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public String[] getCity() {return city;}public void setCity(String[] city) {this.city = city;} }

五、HelloController

1、使用注解@Value映射

@value注解將配置文件的值映射到Spring管理的Bean屬性值

package com.william.day01_springboot_initializr.controller;import com.william.day01_springboot_initializr.domain.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author :lijunxuan* @date :Created in 2019/6/27 17:20* @description :* @version: 1.0*/ @RestController public class HelloController {@AutowiredPerson person;@Value("${person.name}")private String name;@Value("${person.age}")private String age;@RequestMapping("/hello")public String hello(){return String.format("hello world 你好!世界!1234222 name = %s!! age = %s!! person = %s !!",name,age,person);// return String.format("hello world 你好!世界!1234222 person = %s !!",person);} }

七、測試 結果

總結

以上是生活随笔為你收集整理的SpringBoot配置文件与配置类的属性映射方式的全部內容,希望文章能夠幫你解決所遇到的問題。

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