Home > Java > javaTutorial > What are the functions and details of the Spring Boot configuration file?

What are the functions and details of the Spring Boot configuration file?

WBOY
Release: 2023-05-12 22:13:04
forward
1625 people have browsed it

    1. The role of the configuration file

    The configuration file is very important. All important data in the entire project is configured in the configuration file. For example:

    • Database connection information (user name and password settings);

    • Port for project startup;

    • Secret key information for third-party system calls;

    • Ordinary logs and abnormal logs that can detect and locate problems (level and persistence of logs);

    If there is no configuration file, this information cannot be configured, the database cannot be connected, the log cannot be viewed, etc. These problems are very critical to the Spring Boot project, so the configuration file is particularly important. !

    2. Format of configuration files

    The classification of configuration files is generally:

    • System configuration files (defined by the system), such as connection String, log related settings;

    • User-defined;

    The Spring Boot configuration file mainly has two formats:

    • .properties

    • .yml

    What are the functions and details of the Spring Boot configuration file?

    That’s it There are two formats, and it must be written like this here, otherwise an error will be reported (convention is greater than configuration). In both of them, relevant configuration files can be set. The two formats are just products of different times. The writing methods of the other two formats are also different. There will be relevant introduction below. Moreover, these two formats can also coexist. If both are configured, the configuration here will use the configuration in properties and ignore the configuration in yml. configuration, so it is not recommended to use both formats in the same project at the same time!

    3. Description of the properties configuration file

    The properties configuration file is an earlier configuration file format and is also the default configuration file for the Spring Boot project. The following is a detailed introduction to the properties configuration file:

    1. Basic syntax of properties

    In properties, key-value pairs are mainly used (key=value), similar to

    What are the functions and details of the Spring Boot configuration file?

    format (do not write spaces in the middle, and do not write extra spaces)

    If it is a custom configuration file, it is also in the form of key=value, and this needs to be written by yourself. If you want to read this Files can only use their own defined key values ​​to get value values!

    2. Read the configuration file

    This problem can also be solved by using annotations. Here, you can use the @Value annotation and the format of ${} to read:

    @Controller
    public class Test {
        @Value("${server.port}") //读取配置项
        private  String port;
        @ResponseBody  //返回一个非静态页面的数据
        @RequestMapping("/func")  //设置路由地址
        public String  func(){
            return "port: " + port;
        }
    }
    Copy after login

    Then search http://localhost:8080/func through the page to see if the port number read is observed:

    What are the functions and details of the Spring Boot configuration file?

    and using @Value is only If you can read one configuration file, if you want to read multiple, you need to write a few more @Value tags

    3. Disadvantages of properties

    In properties, key is used =value form, and if it is a value under the same key, you need to write the key value repeatedly, which is very redundant

    What are the functions and details of the Spring Boot configuration file?

    and want to solve it This problem requires the use of yml configuration files

    IV. Description of yml configuration files

    yml is another format of configuration file. yml is highly readable and easy to understand. Used to express the format of data serialization, the syntax of yml is very similar to that of high-level languages, and the biggest advantage of yml is that it can be used across languages. For example, python and golang can use yml as a configuration file. The following is a detailed introduction to the yml configuration file:

    1. Basic syntax of yml

    The basic syntax in yml is: key: value. Note that an English colon and a space are required between key and value, where The spaces cannot be omitted. It will be much more convenient to write the above connection database configuration with syntax like

    What are the functions and details of the Spring Boot configuration file?

    ##:

    What are the functions and details of the Spring Boot configuration file?

    Additional: Issues about single and double quotes in yml: If double quotes are used in yml, they will be executed according to the (original) semantics. If they are not added, or single quotes are added, the string will be converted by default. Escape the special characters in, such as \n -> \n (escape) processing.

    2. Read the configuration file

    The way to read the yml configuration file is also the same as the properties, both using the format of @Value annotation plus ${}, which will not be demonstrated here. Let’s take a look at the differences in yml!

    3.配置对象

    在yml里面还是可以配置对象的:

    What are the functions and details of the Spring Boot configuration file?

    另外也可以使用行内写法:

    What are the functions and details of the Spring Boot configuration file?

    而想要读取到这个对象的话就不能再使用@Value注解了,这里需要借助@ConfigurationProperties来读取:

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    @Data
    @ConfigurationProperties(prefix = "student") //读取配置文件的对象
    @Component
    public class Student {
        private int id;
        private String name;
        private int age;
    }
    Copy after login

    然后读取对象,就可以直接进行注入了:

    @Controller
    public class Test {
        @Autowired
        private Student student;
        @ResponseBody
        @RequestMapping("/getstudent")
        public String getStudent(){
            return "student: " + student;
        }
    }
    Copy after login

    然后通过浏览器搜索http://localhost:8080/getstudent就可以得到对象内容了:

    What are the functions and details of the Spring Boot configuration file?

    4.配置集合

    在yml里面也是可以配置集合的:

    What are the functions and details of the Spring Boot configuration file?

    同样行内写法也是可以的:

    What are the functions and details of the Spring Boot configuration file?

    而要获取到集合的话,和获取对象是差不多的,也是通过@ConfigurationProperties来获取:

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    import java.util.List;
    @Data
    @ConfigurationProperties(prefix = "dbtypes")
    @Component
    public class ListConfig {
        private List<String> name;
    }
    Copy after login

    读取集合:

    @Controller
    public class Test {
        @Autowired
        private ListConfig listConfig;
        @ResponseBody
        @RequestMapping("/getlist")
        public String getList(){
            return "name:" + listConfig.getName();
        }
    }
    Copy after login

    然后通过浏览器搜索http://localhost:8080/getlist就可以得到对象内容了:

    What are the functions and details of the Spring Boot configuration file?

    五.properties和yml的区别

    • properties是以key=value的形式配置的键值对类型配置文件,而yml是以key: value的形式配置的键值对类型配置文件;

    • properties是早期且系统默认的配置文件格式,但配置存在一定的冗余数据,而yml可以更好的解决数据冗余问题;

    • yml通用性更好,支持多语言;

    • yml支持更多的数据类型;

    The above is the detailed content of What are the functions and details of the Spring Boot configuration file?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template