zoukankan      html  css  js  c++  java
  • (03) spring Boot 的配置

    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();
        }
        
    }
  • 相关阅读:
    python_控制台输出带颜色的文字方法
    模拟数据库作业
    js笔记
    CSS 笔记
    html 笔记
    必备技能-Git 使用规范流程
    python 闭包
    30个python编程技巧!
    python 面向对象
    python 线程
  • 原文地址:https://www.cnblogs.com/shihuibei/p/9458013.html
Copyright © 2011-2022 走看看