Spring Boot Toggle configuration file to Yaml

Source: Internet
Author: User
Tags redis

The company has recently split online services, naturally using the Spring family Hall-level micro-service Framework--spring boot, which discards the cumbersome XML configuration files in spring, and the declarative annotation approach provides a quick and concise way to configure service development. In spring boot, we find that we don't actually have to do some basic configuration to run the project we just created, because it incorporates a lot of basic generic configuration components without having to do some repetitive configuration work on our own.

In spring boot, we can add custom configuration information to the application through the properties or YAML files. Take Redis's configuration information as an example:

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=5000

You can write the information in the Redis.properites file, and then inject the profile information in the Redisconfig class by @propertysource (value= "Classpath:redis.property"). It is then injected into the corresponding properties of the bean, as follows

@Value ("Spring.redis.host")
  private String host;
  @Value ("Spring.redis.port")
  private int port;
  @Value ("Spring.redis.timeout")
  private int timeout;
But this is a cumbersome approach, and we can actually configure it by introducing a Yaml file (similar to the JSON-like structure), automatically introducing the Snakeyaml when the new spring boot project is created, automatically enabling YAML support. All we need to do is configure the following:

Application.yaml

Redis:
  host:127.0.0.1
  port:6379
  timeout:5000

To create a new redissettings bean:

@Component
@Data
@ConfigurationProperties (prefix = "Redis") public
class Redissettings {
  private String host;
  private int port;
  private int timeout;

}
Finally, the entrance to use can be injected

@Autowired
  private redissettings redissettings;

Summarize:

Even with the use of configuration files, we can find the simplicity and convenience of spring boot. It's a powerful attack on people who criticize Java too heavily.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.