在注解中
如果一个类,上面声明了 @ConfigurationProperties(prefix = "studuent"),则在application.properties中,以student开头的字段,会自动注入到本类中
@ConfigurationProperties,既可以用在配置类上,也可用在bean上。
@Data @AllArgsConstructor @NoArgsConstructor @ToString //@Component @ConfigurationProperties(prefix = "user") public class User { private Integer id; private String name; private String address; }
@Configuration public class UserConfig { @Bean @ConfigurationProperties(prefix = "user") public User user() { return new User(1,"xiaoming","beijing" ); } }
application.properties
server.port=80
user.id = 11111
user.name=xiaoming
user.user-name = liming // 如果有驼峰,自动改为中杠
user.hobby=LOL,DNF,GAME //数组
user.lists=GAME,BOOK,EAT //list 起的名字是lists,非必须是lists
user.maps.k1=v1 //map
user.maps.k2=v2
user.sets=LOL,DNF,LOL,DNF //set
#user.birth.time = 111111123423434
user.birth = 2021/2/4 20:28:30 //日期