I heard that if you search the public account “Java Fish boy” on wechat, you will improve your skills to the next level

(1) Overview

SpringBoot uses a global configuration file to change the default value of SpringBoot automatic configuration. SpringBoot provides configuration files in three formats:If a project has all three of the above configuration files, only application*.yml will be executed, and if there are only YAMl and Properties, only YAMl will be executed.

Yml and YAML have the same format. Key: space value Note that this space cannot be omitted

server:
  port: 8080
Copy the code

Properties is in the key=value format

server.port=8080
Copy the code

(2) Where does the configuration file come from?

The springboot configuration file contains a large number of configurations, which can be seen on the official website: docs.spring. IO /spring-boot…

If you want to use the time to go directly to the official document or Baidu is indeed a way, but it is not the best way, the best way is to understand the principle of these configuration files, later to use the time directly through the principle to write configuration files.

As we learned in the previous chapter on autowaging, Springboot projects go to meta-INF/Spring.Factories to get configuration information when they startThe source of configuration files also comes from here.

RedisAutoConfiguration (” RedisAutoConfiguration “, “RedisAutoConfiguration”, “RedisAutoConfiguration”, “RedisAutoConfiguration”, “RedisAutoConfiguration”, “RedisAutoConfiguration”)After you can see a point in @ EnableConfigurationProperties annotations, and he will bring back a class called XXXProperties, called RedisProperties Redis here. XXXProperties is the heart of the configuration file. @ EnableConfigurationProperties mean let @ ConfigurationProperties annotation class effect, we can see laterXXXProperties this class be @ ConfigurationProperties annotations, and in front of the @ EnableConfigurationProperties corresponding, @ConfigurationProperties loads configuration files (such as applicaition.yaml) that populate the corresponding fields of the object and make them available to other beans. Here we need to write a prefix, which is passed by the “prefix in the configuration file. The “variable” form configures the corresponding value.

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

   /** * Database index used by the connection factory. */
   private int database = 0;
   /** * Connection URL. Overrides host, port, and password. User is ignored. Example: * redis://user:[email protected]:6379 */
   private String url;
   /** * Redis server host. */
   private String host = "localhost";
   /** * Login password of the redis server. */
   private String password;
   /** * Redis server port. */
   private int port = 6379;
   /** * Whether to enable SSL support. */
   private boolean ssl;
   /** * Connection timeout. */
   private Duration timeout;
   /** * Client name to be set on connections with CLIENT SETNAME. */
   private String clientName;
   private Sentinel sentinel;
   private Cluster cluster;
   private final Jedis jedis = new Jedis();
   private final Lettuce lettuce = new Lettuce();
   //...
Copy the code

I think you already know how a configuration file works. When we write a configuration file, we must assign a value to a class variable. We can write a configuration file by “prefix”. I’m going to write it as a variable. Using the code above as an example, if we want to give redis an address, “prefix. The “variable” is spring.redis. Host, which is picked up by RedisProperties,

(3) Summary of configuration file processing

  1. The XXXAutoConfiguration auto-assembly class can be found in meta-INF/Spring.Factories
  2. All properties that can be configured in a configuration file are encapsulated in the xxxxProperties class;
  3. What can be configured in a configuration file to refer to the property class corresponding to a particular function

(4) write a configuration class

Now that we know how SpringBoot fetches configuration files, we can write our own configuration class to read the configuration. Create a new Package Properties class called UserProperties.

@Data
@Component
@ConfigurationProperties(prefix = "myproperties.user")
public class UserProperties {
    private String name;
    private Integer age;
}
Copy the code

You must also enable @ConfigurationProperties when you use it. I’m using @Component for The Component Scan. If you write an @ConfigurationProperties annotation, you’ll get an error

Not registered via @EnableConfigurationProperties, marked as Spring component, or scanned via @ConfigurationPropertiesScan
Copy the code

Then assign the value in the configuration file, which I used application.yaml

myproperties:
  user:
    name: javayz
    age: 23
Copy the code

Test by test method:

@SpringBootTest
class SpringbootdemoApplicationTests {
    @Autowired
    UserProperties userProperties;
    @Test
    void contextLoads(a) { System.out.println(userProperties); }}Copy the code

Instead of using @Component to activate a configuration annotation, we can also activate it in the same way the source code is writtenNow remove the @Component annotation for UserProperties, create a new Package Config, and create a new class called UserConfig

@Configuration
@EnableConfigurationProperties(UserProperties.class)
public class UserConfig {}Copy the code

Run the test class and get the same correct results. That’s the benefit of reading source code, learning how others code.

(5) Conclusion

If you read the whole article, I believe you have a deep understanding of SpringBoot configuration files and the principle of automatic configuration. It is not difficult to read the source code sometimes.