3 ways for SpringBoot to read yml files
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
Bean:
public class MyBean { @Value("${name}") private String name; }
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"); }
3.@ConfigurationProperties annotation
We can use @ConfigurationProperties Annotations map values in yml to bean properties, such as:
application.yml:
my: name: Zhangsan age: 18
Bean:
@Component @ConfigurationProperties(prefix = "my") public class MyProps { private String name; private int age; // getter and setter }
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; }
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
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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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.

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

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

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

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
