标签:设置 ebs get name ati val setname return 两种
Spring Boot内置的配置项远远不能支撑我们的程序运行,在项目设计的时候,往往因为扩展性的需要,项目需要预留很多自定义设置项,Spring Boot允许我们配置自定义选项。
@ConfigurationProperties 类型安全加载
app.name=demoapp
app.version=1.0.0
app.description=demoapp description
app.page-size=20
app.show-advert=true
app.website=http://www.itlaoqi.com
@SpringBootApplication
//在入口类启动时加载config.properties
@PropertySource("classpath:config.properties")
public class MyspringbootApplication {
public static void main(String[] args) {
// SpringApplication.run(MyspringbootApplication.class, args);
SpringApplication app = new SpringApplication(MyspringbootApplication.class);
//关闭Banner
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
@Controller
public class MyController {
@Value("${app.name}")
private String name;
@Value("${app.page-size}")
private Integer pageSize;
...
@Component
//将所有app前缀的属性自动赋值给对应Bean 属性
@ConfigurationProperties(prefix="app")
public class AppConfig {
private String name;
private String description;
private String version;
private Integer pageSize;
private Boolean showAdvert;
private String website;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
...
使用时动态注入
@Controller
public class MyController {
//动态注入IOC容器中匹配的Bean
@Resource //相同功能可使用@Autowire
private AppConfig appConfig;
...
标签:设置 ebs get name ati val setname return 两种
原文地址:https://www.cnblogs.com/itlaoqi/p/11391749.html