SpringBoot 读取自定义配置文件

在SpringBoot下读取自定义properties配置文件的方法 

@Value 注入 List Map

读取配置文件my.properties

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

 指标映射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测试

@RestController
public class TestController {

    @Autowired
    User user;

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

 pom.xml  添加依赖

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

 添加lombok插件

 启动测试打印效果

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

 文本乱码解决

ctrl+alt+s 打开settings

猜你喜欢

转载自blog.csdn.net/xiangwang2016/article/details/104991214