一、获取自定义配置的值
application.properties配置
book.name=springboot
@Controller或@Service组件配置
@Value("${book.name}") private String name;
二、获取类型安全的自定义配置的值
application.properties配置
book.name=springboot
@Controller或@Service组件配置
@ConfigurationProperties(prefix="book") public class BookController { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
三、配置文件定义集合类型值
配置文件
#对象配置 #person: {name: huangtingting,age: 19} person: name: mengmeiqi age: 18 #集合配置 #persons: [{name: huangtingting,age: 18},{name: jujingyi,age: 19}] persons: - name: jujingyi age: 18 - name: huangtingting age: 19
组件
package com.wuxi.bean; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @Data @ConfigurationProperties(prefix = "person")//注入配置的值 @Component public class Person { private String name; private Integer age; private List<Map<String, String>> persons; }
四、profile配置
application.properties配置
spring.profiles.active=dev
新建application-dev.properties和application-pro.properties