SpringBoot中自定义配置类
1.方式一:
配置类标注:
- @ConfigurationProperties注解,可以设置前缀名
- @Component 将该类放入spring容器中
- 必须设定get/set方法
配置类:
@Component //将该Bean放入spring容器中
@ConfigurationProperties(prefix = "com.example")//指定该类为配置获取类,并指定前缀
public class MyConfig {
private String name;
private Integer age;
//必须设置get set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
application.properties中配置
com.example.age=20
com.example.name=xxx
使用:
在其他Bean中可以直接注入该Bean
@RestController
public class DemoController {
@Autowi