zoukankan      html  css  js  c++  java
  • Springboot(二)-application.yml默认的配置项以及读取自定义配置

    写在前面

    =====

    spring-boot 版本:2.0.0.RELEASE

    =====

    读取自定义配置

    1.配置文件:sys.properties

    supply.place=云南
    supply.code=002

    2.配置类:SupplyConfig.java

    package com.example.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @PropertySource(value = "classpath:/sys.properties")
    @ConfigurationProperties(prefix = "supply")
    public class SupplyConfig {
    
    
        private String place ;
        private String code;
    
        public String getPlace() {
            return place;
        }
    
        public void setPlace(String place) {
            this.place = place;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    }

    因为spring-boot使用了2.0.x,所以@ConfigurationProperties 没有locations的属性,改为使用@PropertySource注解代替配置文件路径

    3.测试接口

    @RestController
    @RequestMapping("/test")
    public class HelloWorldController{
        @Autowired
        SupplyConfig supplyConfig;
    
    
        @RequestMapping("supply/code")
        public String getSupplyCode() {
            return supplyConfig.getCode();
        }
        @RequestMapping("supply/name")
        public String getSupplyName() {
            return supplyConfig.getPlace();
        }
    }

    问题1:中间出现读取中文配置出现乱码问题,在主文件application.yml 进行编码配置可以解决

    server:
      tomcat:
        uri-encoding: UTF-8
    spring:
      http:
        encoding:
          charset: UTF-8
          enabled: true
          force: true
      messages:
        encoding: UTF-8

    问题2:配置文件使用.yml格式的暂时还遇到问题,读取不到配置

  • 相关阅读:
    《Java程序设计》第五周学习总结
    团队建设任务
    《Java程序设计》第四周学习总结
    ML_Review_GMM(Ch10)
    ML_Review_SVM(Ch9)
    ML_Review_LDA(Ch5)
    ML_Review_PCA(Ch4)
    关于Kernel的思考
    ML_Homework_Porject_2_LDA_KNN
    CV_Learn
  • 原文地址:https://www.cnblogs.com/cici20166/p/8697770.html
Copyright © 2011-2022 走看看