zoukankan      html  css  js  c++  java
  • SpringBoot 读取配置文件及profiles切换配置文件

    读取核心配置文件

    核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。

    先创建一个简单的springBoot程序,可以参考:
    http://www.cnblogs.com/lspz/p/6344327.html

    一、通过@value注解来读取

    核心配置文件application.properties内容如下:

    server.port=9090
    
    test.msg=Hello World Springboot!
    

    编制Example.java

      package com.example.web;
    
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
    
    	@RestController
    	public class Example {
    	
    	    @RequestMapping("/")
    	    public String home(@Value("${test.msg}") String msg) {
    	        return msg;
    	    }
    	}
    

    注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody,spring boo默认已经配置了很多环境变量,例如,tomcat的默认端口是8080,项目的contextpath是“/”等等,我们在application.properties中设置了server.port=9090,重写了spring boot 内嵌tomcat端口。

    访问:http://localhost:9090 时将得到 Hello World Springboot!

    二、使用Environment方式

     package com.example.web;
    
    	import org.springframework.beans.factory.annotation.Autowired;
    	import org.springframework.core.env.Environment;
    	import org.springframework.web.bind.annotation.RequestMapping;
    	import org.springframework.web.bind.annotation.RequestMethod;
    	import org.springframework.web.bind.annotation.RestController;
    	
    	@RestController
    	public class WebController {
    	    @Autowired
    	    private Environment env;
    	
    	    @RequestMapping(value = "index", method = RequestMethod.GET)
    	    public String index() {
    	        return env.getProperty("test.msg");
    	    }
    	}
    

    注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上@Autowired注解即可完成依赖注入,然后使用env.getProperty("键名")即可读取出对应的值。

    访问:http://localhost:9090/index 时将得到Hello World Springboot!

    三、读取自定义配置文件

    为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件my-web.properties

    resources/my-web.properties内容如下:

    com.name=testName
    com.password=123
    

    创建管理配置的实体类:

    package com.example.model;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @PropertySource("classpath:/my-web.properties")
    @ConfigurationProperties(prefix = "com")
    public class ConfigBean {
        private String name;
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    注意:

    spring boot1.5以上版本@ConfigurationProperties取消了location需要用@PropertySource来指定自定义的资源目录。

    prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以web.开头)

    使用@Component是让该类能够在其他地方被依赖使用,即使用@Autowired注释来创建实例。

    创建测试Controller

    package com.example.web;
    
    import com.example.model.ConfigBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
        @Autowired
        ConfigBean configBean;
    
        @RequestMapping("/user")
        public String user() {
            return configBean.getName() + ":" + configBean.getPassword();
        }
    }
    

    由于在ConfigBean类上加了注释@Component,所以可以直接在这里使用@Autowired来创建其实例对象。

    访问:http://localhost:9090/user 时将得到testName:123

    四、参数间引用

    可以利用${…}在application.properties引用变量
    myapp.name=spring
    myapp.desc=${myapp.name} nice

    五、在application.properties配置随机变量

    在application.properties配置随机变量,利用的是RandomValuePropertySource类

    my.secret=${random.value}
    my.number=${random.int}
    my.bignumber=${random.long}
    my.number.less.than.ten=${random.int(10)}
    my.number.in.range=${random.int[1024,65536]}
    

    六、使用profiles实现快速切换配置

    新建一个properties文件application-prod.properties对应为
    生产环节的配置。

    application-prod.properties:

    server.port=8080
    test.msg=This is prod!
    

    接下来,使用CMD进入src目录打包jar:

    mvn package -Dmaven.test.skip=true

    success后使用

    java -jar -Dspring.profiles.active=prod target/loadProperties-0.0.1-SNAPSHOT.jar运行。

    访问:http://localhost:8080/ 时得到 This is prod!

    注意:

    发现端口已经变成8080了。这是因为在“application-prod.properties”中规定了server.port=8080。

    “java -jar”的命令中使用-D来传递参数:
    java -jar -D配置=值 jar名.jar
    “-Dspring.profiles.active=”用来指定切换到哪个配置,表达式为:“application-${profile}.properties”

    七、配置文件优先级

    application.properties和application.yml文件可以放在一下四个位置:

    外置,在相对于应用程序运行目录的/congfig子目录里。
    外置,在应用程序运行的目录里
    内置,在config包内
    内置,在Classpath根目录

    同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.yml里面的属性就会覆盖application.properties里的属性。

  • 相关阅读:
    对于函数中多个返回值的处理
    Docker-compose 安裝单机版redis
    设计模式七大设计原则
    UML 设计技巧
    使用Docker 容器配置nexus3.29 私有仓库
    分布式消息Kafka通信原理分析
    分布式消息Kafka通信
    使用docker 搭建nexus3.29
    分布式消息Kafka初步认识及基本应用
    Dubbo 常用配置及源码分析
  • 原文地址:https://www.cnblogs.com/lspz/p/6831669.html
Copyright © 2011-2022 走看看