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”)
使用注解@ConfigurationProperties映射
通过注解@ConfigurationProperties(prefix=’'配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映 射。
使用@ConfigurationProperties方式必须提供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配置文件与配置类的属性映射方式相关推荐
- springboot在yml配置文件中配置类的属性笔记
首先建立一个简单的实体类,我这里以学生为例,并加上@Component和@ConfigurationProperties(prefix ="student")注解,其中prefix ...
- SpringBoot中使用yml配置文件以及配置类实现文件上传下载路径的修改
场景 SpringBoot+thymeleaf实现文件下载或者实现文件上传需要配置文件上传路径的地方, 不要写为固定路径,在配置文件中指定文件路径,代码中直接引用. 避免以后文件路径修改后需要修改业务 ...
- SpringBoot配置文件YAML配置注入(详解)
目录 一.SpringBoot配置文件 1. SpringBoot默认配置文件 2. 配置文件的作用 3. 配置文件的位置 4. 多环境切换 方式一:多配置文件 方式二:一个配置文件(yaml的多文档 ...
- 【SpringBoot】SpringBoot配置文件
上次说完SpringBoot项目搭建的流程和原理,这节写一下SpringBoot的配置文件的相关知识. 1.配置文件类型和作用 SpringBoot的设计思路就是约定大于配置,很多配置都是设定好的,不 ...
- springboot配置文件_SpringBoot系列干货:配置文件详解
在Spring Boot中,配置文件有两种不同的格式: 1.一个是application.properties server.port=8888 server.servlet.context-path ...
- apache 配置文件内使用 8080 端口_【SpringBoot 框架】- SpringBoot 配置文件
一.SpringBoot配置文件类型 SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话 ,就可以自己编写配置文件进行相应配置,起步依赖spring-bo ...
- spring.xml配置类属性--喜闻乐见
相信大家在开发的过程中,都会写一些配置文件或者配置类来,毕竟好的编码习惯是不能硬编码的,所以配置文件和配置类就显得很重要了.但是我用久了之后发现,配置文件和配置类确实好用,但是假如有多个配置的话,那么 ...
- SpringBoot配置文件数据库密码加密
引言 需求:springboot的配置文件中,把连接数据库的密码加密,使之不是以明文存储 步骤 导入Maven坐标 <dependency><groupId>com.githu ...
- 32位数据源中没有mysql_[SpringBoot实战]快速配置多数据源(整合MyBatis)
前言 由于业务需求,需要同时在SpringBoot中配置两套数据源(连接两个数据库),要求能做到service层在调用各数据库表的mapper时能够自动切换数据源,也就是mapper自动访问正确的数据 ...
最新文章
- 分布式多层次限流概述
- WPF 仿IPhone滑块开关 样式 - CheckBox
- 文巾解题 19. 删除链表的倒数第 N 个结点
- tmux与python虚拟环境问题
- 小余学调度:调度禁忌操作讲解(持续更新中ing)
- 无法激活安全认证服务
- Oracle 之 管理
- sap commit rollback
- 监测UITextField的变化
- 刘强东喊出技术转型第二年,京东AI全景图首次披露
- echart单击后获取横坐标值_新、老、离职员工名单只要刷新一下就能轻松获取|Excel125...
- php递归函数理解,详解php递归函数
- 安装 Unity Hub 发现 进不了 Unity3D 了
- css实现气泡框效果
- RepOptimizer学习笔记
- 漏刻有时数据可视化Echarts组件开发(21):基于echarts开发的自动旋转map3D下钻和柱图地图
- gis连接表格到数据库失败_arcgis连接到数据库失败,常规功能故障
- windows 配置host
- 【For my liz】宇宙制作全纪录(如果能成功的话TT)
- 罕见病、新药最新研究进展(2021年9月)
热门文章
- 网络研讨室_即将举行的网络研讨会:调试生产中Java的5种最佳实践
- kafka streams_Kafka REST Proxy for MapR Streams入门
- javafx有布局管理器吗_JavaFX技巧17:带有AnchorPane的动画工作台布局
- 布线问题分支限界法java_大型布线:Java云应用程序缺少的技术
- Java,JavaFX的流畅设计风格滑块
- JDK 11:新的默认收集方法toArray(IntFunction)
- Spring,Reactor和ElasticSearch:使用伪造的测试数据进行标记
- 用Hamcrest验证DateTime和日期
- UltraESB的首选IDE – IntelliJ IDEA
- Apache Lucene 5.0.0即将发布!