SpringBoot使用@ConfigurationProperties注解读取yaml文件中配置信息

背景:为了更好的测试,你就得更好地了解开发,为了更好的了解开发,你就知道开发常用框架,那就来吧,第一个springboot
目的:使用注解读取application.yml配置文件中信息
组网图:不涉及
工具:java version “1.8.0_65” ;Apache Maven 3.6.3;IDEA版本 2018.3 (准备步骤见本人其它博文)
步骤:
使用@ConfigurationProperties注解读取application.yml文件,如下
在这里插入图片描述

server:
  port: 8080
  servlet:
    context-path: /v1

woman:
  age: 25
  girl: 那个我喜欢的人
  attribute: beautiful

添加WomanConfig类
在这里插入图片描述

package com.hua.myfirst;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@ConfigurationProperties(prefix = "woman")
public class WomanConfig {

    private BigDecimal age;

    private String girl;

    private String attribute;

    public BigDecimal getAge() {
        return age;
    }

    public void setAge(BigDecimal age) {
        this.age = age;
    }

    public String getGirl() {
        return girl;
    }

    public void setGirl(String girl) {
        this.girl = girl;
    }

    public String getAttribute() {
        return attribute;
    }

    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
}

使用@ConfigurationProperties报错:Spring Boot Configuration Annotation Processor not found in claspath
在这里插入图片描述
解决方法参考此博客:https://blog.youkuaiyun.com/li1325169021/article/details/91984562

更改HelloConteoller类
在这里插入图片描述

package com.hua.myfirst;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloConteoller {

    @Autowired
    private WomanConfig womanConfig;

    @GetMapping("/hello")
    public String hello() {
        return "She is " + womanConfig.getAttribute() + "girl";
    }
}

启动springboot
打开链接:http://127.0.0.1:8080/v1/hello
在这里插入图片描述
备注:pom.xml文件如下

<?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 https://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.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.hua</groupId>
	<artifactId>myfirst</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>myfirst</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-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot</artifactId>
			<version>2.1.3.RELEASE</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.4.2</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

启动类如下:

package com.hua.myfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyfirstApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyfirstApplication.class, args);
	}

}

Spring Boot项目中,通过@ConfigurationProperties注解读取YAML格式的配置文件,特别是复杂的数据结构,是一项基础而关键的技能。首先,确保项目中已经引入了`spring-boot-configuration-processor`依赖,以便编译时自动生成元数据,使得配置文件中的属性能够映射到Java Bean中。接着,在`application.yml`文件中定义你的属性,例如使用列表和映射这样的复杂数据结构。然后,创建一个Java类,使用`@Component`和`@ConfigurationProperties`注解标记该类,并通过`prefix`属性指定配置文件中对应的顶级配置名称。在这个类中定义相应的字段,Spring Boot会自动将配置文件中相应的属性值注入到这些字段中。如果你使用的是复杂的数据结构,比如List和Map,需要确保它们的字段类型能够正确地匹配配置文件中的数据结构。最后,在需要使用这些配置的类中,通过`@Autowired`注解注入你创建的Bean,从而访问配置文件中的值。通过这些步骤,你可以在Spring Boot项目中灵活地读取使用配置文件中的各种属性。如果希望深入学习更多关于Spring Boot配置的高级用法和最佳实践,建议查看这份资料:《SpringBoot读取application.yml属性值步骤解析》。这份资源将为你提供从基础到进阶的详细指导,帮助你在配置管理方面变得更加熟练和高效。 参考资源链接:[SpringBoot读取application.yml属性值步骤解析](https://wenku.youkuaiyun.com/doc/6412b536be7fbd1778d4257e?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值