zoukankan      html  css  js  c++  java
  • Spring Boot学习(二):配置文件

    前言

    Spring Boot有两中类型的配置文件:.properties和.yml。Spring Boot默认的全局配置文件名为application.properties或者application.yml(spring官方推荐使用的格式是.yml格式,目前官网都是实例都是使用yml格式进行配置讲解的),应用启动时会自动加载此文件,无需手动引入。

    方式1:通过配置绑定对象的方式

    我自定义一个配置文件my.properties

    student.name = ZhangSan
    student.age = ${random.int[1,30]}
    student.parents[0] = ZhangMing
    student.parents[1] = XiaoHua
    

    创建一个Bean,用于自动读取配置文件的内容(注:需保证与配置文件的字段一致)

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 2018/9/28.
     */
    @Component
    @PropertySource(value = "classpath:/my.properties", encoding="utf-8") //设置系统加载自定义的配置文件,并指定配置文件编码格式
    @ConfigurationProperties(prefix = "student")
    @Data
    public class Student {
    
        private String name;
    
        private int age;
    
        private List<String> parents;
    }
    
    import com.jc.testConf.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by Administrator on 2018/6/26.
     */
    @RestController
    public class HelloController {
    
        @Autowired
        Student student;
    
        @RequestMapping("/testConf")
        private String getStudent(){
            return student.toString();
        }
    
    }
    

    测试,果然能够获取到配置文件的数据

    所以,以后,就可通过配置对象来获取相关的配置即可。

    方式2:@Value("${blog.author}")的形式获取属性值

    import com.jc.testConf.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by Administrator on 2018/6/26.
     */
    @RestController
    public class HelloController {
    
        @Value(value = "${student.name}")
        String name;
    
        @RequestMapping("/studentName")
        private String studentName(){
            return name;
        }
    
    }
    

    测试可以获取

    相关说明

    在学习配置中,设计到其他的知识点,所有在文章末尾补充下。

    注解@Value的说明

    有三种使用方式:

    • 基本数值的填充,如:@Value("12")、@Value("张三")
    • 基于SpEl表达式#{} 如上文中年龄也可以这样#{28-2},如:@Value("#{car.luntai}")
    • 基于配置文件${配置文件中参数名},如:@Value("${database.source.url}")

    参考

    https://blog.lqdev.cn/2018/07/14/springboot/chapter-third/
    http://blog.51cto.com/4247649/2118348

  • 相关阅读:
    xpath解析以及lxml解析库
    python sort()和sorted()的不同
    爬取电影天堂-二级页面抓取
    爬取猫眼电影榜单TOP100榜-以mysql数据库保存
    爬取猫眼电影榜单TOP100榜-以csv文件保存
    爬取猫眼电影榜单TOP100榜-以命令行输出
    爬虫 贪婪匹配以及非贪婪匹配
    爬取百度贴吧
    python 面试
    python 从array保存伪色彩图片 —— 发现的坑
  • 原文地址:https://www.cnblogs.com/wsygdb/p/9724451.html
Copyright © 2011-2022 走看看