Note 04-- springboot reads the configuration file used slf4j + log

Foreword

springboot common configuration file has yml and properties are two, of course, when necessary, can also be used xml. Personally, I prefer using yml, so I use yml as an example here. yml or properties configuration file can do a lot of work for us, we just want to read the relevant attribute values ​​with them on the line in the configuration file.

yml Syntax Reference

+ Idea reads the configuration file using the logging Slf4j

1, idea to install lombok plug (slf4j required)

file -> settings -> plugins

Find lombok install, then reboot idea.


2, create a springboot project, the introduction of its dependencies

<!-- 使用@ConfigurationProperties-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok依赖 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>


2, modify application.yml

person:
  name: Kobe Bryant
  age: 18
  salary: 1235478.22
  car:
    carName: 奔驰S3200
    carBrand: 奔驰
  hobbit:
    - 篮球
    - singing
    - dancing
  daughters:
    k1: d1
    k2: d2
  birthDate: 1980/01/01
  marriage: true


#时间戳统一转换
spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss

yml Syntax Reference


3, create entity classes

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component //导入Spring IOC容器中管理
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
    private String name;
    private int age;
    private double salary;
    private Car car;
    private List<String> hobbit;
    private Map<String, Object> daughters;
    private Date birthDate;
    private Boolean marriage;
}

@ConfigurationProperties(prefix = "person")

Mapping profile to the entire object, into bean.

It can be used with @PropertySource mark, which specify all the configuration file mapping.

If we do not specify the address of the file attributes, Springboot default properties file name read application.properties/application.yml in.

E.g:

@PropertySource("classpath:xxx.yml")
@ConfigurationProperties(prefix = "person")

It will map xxx.yml to person prefixed properties.


Car.java

import lombok.Data;
@Data
public class Car {
    private String carName;
    private String carBrand;
}

ps: @Data is lombok annotations using the annotation will be automatically generated getter, setter, toString method for the entity classes at runtime.

refer to the specific use lombok


3. Create a control class


@Slf4j
@RestController
@EnableConfigurationProperties(Person.class)

public class PropertiesController {
    @Autowired
    private Person person;

    @RequestMapping(value="/getProperties")
    public Person getProperties() {
        log.info("/getProperties");
        return person;
    }
}

@EnableConfigurationProperties (Person.class) role is to make the annotation phrase ConfigurationProperties into effect, the Person class is injected into the vessel IOC in spring.

About EnableConfigurationProperties, in comments SpringBoot in this explanation is: to provide effective support for Bean with @ConfigurationProperties annotations. This notation classes provide a convenient way to be injected is annotated with @ConfigurationProperties Bean Spring container.


4, the test

Output profile contents


INFO level log

Guess you like

Origin www.cnblogs.com/Jotal/p/11202632.html