SpringBoot reads custom configuration files

Method of reading custom properties configuration file under SpringBoot 

@Value injects List Map

Read the configuration file my.properties

user.mark=jack
user.age=25
user.address=北京
user.work=北京,上海
user.parents[mother]=冯女士
user.parents[father]=孙先生

 Metric Mapping Bean

@Component 
@Data//注解来自于 lombok,lombok 能够减少大量的模板代码
@Accessors(chain = true)//默认false如果设置为true setter返回的是此对象,方便链式调用方法
@ToString
@ConfigurationProperties(prefix = "user", ignoreUnknownFields = false)
@PropertySource("classpath:config/my.properties")
public class User {
    private String mark;
    private int age;
    private String address;
    private List<String> work;
    private Map<String,String> parents;
}

 

Controller test

@RestController
public class TestController {

    @Autowired
    User user;

    @RequestMapping("/")
    public String getResponseValue() {
        return user.toString();
    }
}

 pom.xml add dependency

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

 Add lombok plugin

 Start test printing effect

User(mark=jack, age=25, address=北京, work=[北京, 上海], parents={mother=冯女士, father=孙先生})

 

 Text garbled resolution

ctrl+alt+s open settings

Guess you like

Origin blog.csdn.net/xiangwang2016/article/details/104991214