SpringBoot学习(2)

来源:互联网 发布:php while continue 编辑:程序博客网 时间:2024/05/24 01:46

1.类型安全的配置(基于properties)

1.1 配置文件中添加属性


1.2 编写类型安全的Bean 

通过@ConfigurationProperties加载properties文件中的配置 , 通过prefix属性指定properties的配置中的前缀
package me.gdy.SpringBoot.bean;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix="author")public class UserInfo {private String name ; private String age ;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;} }

1.3 在主程序中添加如下代码

@Autowiredprivate UserInfo user;@RequestMapping("/user")String userInfo(){return "name is "+user.getName()+" age is "+user.getAge();}

1.4 运行结果


2.日志配置



原创粉丝点击