Table of Contents
The main ways for Spring Boot to read yml files are as follows:
1.@Value annotation
2.Environment object
3.@ConfigurationProperties annotation
4.YmlPropertySourceFactory
5.@YamlComponent annotation
Home Java javaTutorial 3 ways for SpringBoot to read yml files

3 ways for SpringBoot to read yml files

Jun 29, 2023 pm 11:25 PM
yml


The main ways for Spring Boot to read yml files are as follows:

1.@Value annotation

We can add it to the properties of the bean Use the @Value annotation to directly read the value in yml, such as:

application.yml:

name: Zhangsan
Copy after login

Bean:

public class MyBean {
    @Value("${name}")
    private String name;
}
Copy after login

2.Environment object

We can read the yml value by injecting the Environment object, such as:

@Autowired
private Environment environment;
public void doSomething() {
    String name = environment.getProperty("name");
}
Copy after login

3.@ConfigurationProperties annotation

We can use @ConfigurationProperties Annotations map values ​​in yml to bean properties, such as:

application.yml:

my: 
    name: Zhangsan
    age: 18
Copy after login

Bean:

@Component 
@ConfigurationProperties(prefix = "my")
public class MyProps {
    private String name;
    private int age;
    // getter and setter
}
Copy after login

4.YmlPropertySourceFactory

We can use YmlPropertySourceFactory to load the yml file, and then read the value like ordinary Properties, such as:

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(new ClassPathResource("application.yml"));
    factory.getObject().forEach((k, v) -> System.out.println(k + ": " + v));
    return factory; 
}
Copy after login

5.@YamlComponent annotation

If there are multiple documents separated by — in the yml file, we can use the @YamlComponent annotation to map each document to a bean, such as:

application.yml:

my:
  name: Zhangsan 
---
my: 
  name: Lisi
Copy after login

Beans:

@Component("first") 
@YamlComponent(value = "my.first")
public class FirstProps {
    private String name;
}
@Component("second")  
@YamlComponent(value = "my.second")
public class SecondProps {
    private String name; 
}
Copy after login

You can choose 5 main methods according to your needs. Spring Boot can read yml files. yml is the default configuration file format of Spring Boot. Understanding how to manipulate yml files will help us achieve system configuration flexibility.

The above is the detailed content of 3 ways for SpringBoot to read yml files. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does springboot read lists, arrays, map collections and objects in yml files? How does springboot read lists, arrays, map collections and objects in yml files? May 11, 2023 am 10:46 AM

application.yml defines the list collection. The first way is to use the @ConfigurationProperties annotation to obtain all the values ​​​​of the list collection type:code:status:-200-300-400-500. Write the entity class corresponding to the configuration file. What needs to be noted here is that defining the list Collection, first define a configuration class Bean, and then use the annotation @ConfigurationProperties annotation to obtain the list collection value. Here we will explain the role of the relevant annotations. @Component hands over the entity class to Spring management @ConfigurationPropertie

How to solve the problem of com.mysql.cj.jdbc.Driver becoming popular in yml file in Springboot How to solve the problem of com.mysql.cj.jdbc.Driver becoming popular in yml file in Springboot May 11, 2023 pm 09:52 PM

The reason why com.mysql.cj.jdbc.Driver became popular in the yml file was because I built a framework a few days ago, and a strange problem occurred. When configuring the mysql file, com.mysql.cj.jdbc.Driver kept becoming popular. I thought the version was too low, so I upgraded to a higher version, but it was still popular. Finally, I checked online for a long time. The online method said that the version was too low, but it still didn't work. The dependency package I used before was: mysqlmysql-connector-java8.0.15 Finally, I just removed the version number, mysqlmysql-connector-javadriver-class-na

3 ways for SpringBoot to read yml files 3 ways for SpringBoot to read yml files Jun 29, 2023 pm 11:25 PM

The main ways SpringBoot reads yml files are as follows: 1. @Value annotation We can use the @Value annotation on the properties of the bean to directly read the value in yml, such as: application.yml:name:ZhangsanBean:publicclassMyBean{ @Value("${name}")privateStringname;}2.Environment object We can read the yml value by injecting the Environment object, such as: @AutowiredprivateEnvironmentenv

How does SpringBoot read YML, yaml, and properties files? How does SpringBoot read YML, yaml, and properties files? May 13, 2023 pm 09:07 PM

Yml, yaml, and properties files are all used to store configuration files. Some static data and configuration data will be stored in them. But sometimes we not only need to store static data, but also need to read data from files. These three types of files are placed under project--"src--"main--"resource. If other files need to be stored in the resource, a config package will be created in the resource. Store the configuration file in it. 1. Create a new configuration file in the project. The file storage address bootstrap.yml is a configuration file of the SpringBoot program. It will be loaded before the project starts.

What is the method of multi-level and multi-environment yml design in SpringBoot project? What is the method of multi-level and multi-environment yml design in SpringBoot project? May 30, 2023 pm 11:36 PM

There are some common fixed basic configurations in the demand scenario infrastructure module. For example: log configuration, Spring's own configuration, MyBatisPlus-related fixed configuration, etc. These configurations are often irrelevant to the environment. How can they be reused? #Log configuration logging:level:#Remember to configure the package name com.agileboot:debugorg.springframework:infopattern:console:"%date%thread%green(%level)[%cyan(%logger{10}):%magenta( %line)]%red(%method

How to use Springboot's yml configuration file How to use Springboot's yml configuration file May 11, 2023 pm 12:19 PM

1. Service configuration server:port:8989servlet:#context-path is the path to be added between the port number and the service. For example, /wcm of localhost:8080/wcm/login/login is context-path:/. 2. Database connection spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://127.0.0.1:3306/jxc_manage?useUnicode=true&charac

How does Springboot specify to obtain the configuration values ​​​​in the yml file? How does Springboot specify to obtain the configuration values ​​​​in the yml file? May 15, 2023 am 09:55 AM

Content: Two methods are introduced here, both of which are based on annotations. They are: @Value("${xxxxx.xx}")@ConfigurationProperties(prefix="xxxxx") Enter the topic: @Value("${xxxxx.xx. xx}") It is very simple to use this method (each annotation obtains a corresponding configuration value). Add our custom configuration items in the yml, such as (the case is arbitrary, just correspond when calling): Specify to obtain these values To use, such as: @Value("${myKey.tua}&quo

What are the methods for SpringBoot to read Yml configuration files? What are the methods for SpringBoot to read Yml configuration files? Jun 02, 2023 pm 10:20 PM

1. Basic usage. It is more common to use the @Autowired annotation to inject the Environment class. Just like injecting service or dao, declare an Environment class variable and add the @Autowire annotation. As follows: importorg.springframework.core.env.Environment;@AutowiredprivateEnvironmentenvironment; The method of use is to read the information in the configuration file through the getProperty(key) method. The code is as follows: 1) Configuration heycloud:job in Yml

See all articles