preface

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. Today’s share is not small knowledge, but stepped on the two pits, I first step as respect, I hope you do not enter after reading, do not enter!

Pit point 1

1.1 describe

ConfigurationProperties(prefix = “”) ConfigurationProperties(@configurationProperties (prefix = “”))

The Spring Boot configuration annotation processor could not be found in the classpath

Although it does not affect the injection configuration attribute value, but it affects the code aesthetic, resulting in physical and mental discomfort!

1.2 filling holes

Click, the error text after the link:

Then open this page:

Ah, ok… I’ll just give you two solutions:

  • Open panel Settings (click the wrench icon in the above picture to enter quickly). The operation is as follows:

  • Treating both symptoms and causesIn thepom.xmlAdd to filespring-boot-configuration-processorRely on:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
Copy the code

Pit point 2

2.1 describe

Re: How do properties in configuration files map to static member variables in configuration classes?

Configuration file application.yml ↓

    # Project related configuration
    project:
      # the name
      name: DemoProject
      # version
      version: 1.1. 0
      # Copyright Year
      copyrightYear: 2021
      Windows E:/test/uploadPath, Linux /home/test/uploadPath
      profile: E:/test/uploadPath
Copy the code

Configuration class ProjectConfig ↓

    @Component
    @ConfigurationProperties(prefix = "project")
    public class ProjectConfig {

        /** Project name */
        private String name;

        /** 版本 */
        private String version;

        /** Copyright year */
        private String copyrightYear;

        /** Upload path */
        private static String profile;

        public String getName(a)
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public String getVersion(a)
        {
            return version;
        }

        public void setVersion(String version)
        {
            this.version = version;
        }

        public String getCopyrightYear(a)
        {
            return copyrightYear;
        }

        public void setCopyrightYear(String copyrightYear)
        {
            this.copyrightYear = copyrightYear;
        }

        public static String getProfile(a) {
            return profile;
        }

        public static void setProfile(String profile) {
            ProjectConfig.profile = profile;
        }

        /** * Obtain the import upload path */
        public static String getImportPath(a)
        {
            return getProfile() + "/import";
        }

        /** * Obtain the profile picture upload path */
        public static String getAvatarPath(a)
        {
            return getProfile() + "/avatar";
        }

        /**
         * 获取下载路径
         */
        public static String getDownloadPath(a)
        {
            return getProfile() + "/download/";
        }

        /** * Obtain the upload path */
        public static String getUploadPath(a)
        {
            return getProfile() + "/upload"; }}Copy the code

This configuration class contains four member variables, corresponding to the attributes under project in the configuration file respectively. The first three are common variables, and profile is a static attribute. Since it is frequently used in the later period, the class name is used. Easily and quickly get the global configuration file path.

Note: Don’t leave it out@ComponentA note that willProjectConfigIn theSpringIn the container, handed overSpringManagement.

What’s the difference between @Value and @ConfigurationProperties?

The @Value annotation is an explicit binding that can be configured with a fully qualified name. The @ConfigurationProperties annotation is essentially an implicit binding that uses multiple @Values at the same time, which means that when writing a configuration file, It only needs to be the same as the attribute name in the class. If the attribute name in the class is humped, use a short horizontal (-) concatenation.

As I was running my test case and ready to gloat over the console output projectConfig.getProfile () file path, NULL flashed across my face and I froze.

2.2 filling holes

After trying many methods, it still failed, but I could get non-static values of other attributes through automatic injection. At that time, I decided to seek help from my classmate. I learned from him that it was static attributes. The set method of a profile is used to inject values into other attributes. It turns out that I marked its assignment method static, which prevents it from being called when configuring property injection.

Remove the static keyword and I get the file path I want:

At the end

Writing is not easy, welcome everyone to like, comment, your attention, like is my unremitting power, thank you to see here! Peace and Love.