热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

(03)springBoot的配置

1.springboot的核心配置springboot项目建立之后,已经创建好了application.properties配置文件其实,配置文件还支持*.yml格式的;
1. spring boot 的核心配置

spring boot 项目建立之后,已经创建好了application.properties 配置文件

image

其实, 配置文件还支持*.yml 格式的;

2. 多配置环境的配置文件(实际开发)

application-dev.properties

application-test.properties

application-online.properties

多环境的配置文件, 这时候我们需要在application.properties配置一下, 激活其中某个配置文件, 具体配置如下:

spring.profiles.active=dev (开发环境)

那么问题来了, 如果开发环境中配置端口8089, application.properties中也配置了8080, 最终是生效开发环境8089的端口

3. spring boot的自定义配置文件

在application.properties 里面配置好你想要的配置, 之后在cotnroller中使用@value注解获取自定配置的值

image

然后在controller中获取

image

另外一种读取自定义配置的方法

定义一个类, 然后读取值到这个类的属性中, 之后调用类的属性获取配置

/**
 * 定义自定属性中的值
 */
@Component
@ConfigurationProperties(prefix = "boot")
public class ConfigInfo {
    private String country;
    private int age;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@Controller
public class ConfigController {
    @Value("${boot.country}")
    private String country;

    @RequestMapping("/boot/config")
    public @ResponseBody String config(){
        return country;
    }

    @Autowired
    private ConfigInfo configInfo;
    @RequestMapping("/boot/config2")
    public @ResponseBody String config2(){
        return configInfo.getCountry() + configInfo.getAge();
    }
    
}

推荐阅读
author-avatar
似风似戏是梦而已
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有