美文网首页
SpringBoot 参数映射,开发及线上设置

SpringBoot 参数映射,开发及线上设置

作者: lconcise | 来源:发表于2018-07-13 12:00 被阅读141次

1. SpringBoot 自定义参数映射

(1) 自定义参数映射简介

springboot整体是提倡是用较少的配置文件,如果有些参数,你不得不通过配置文件进行设置,以增加系统线上使用的灵活性。

(2) 自定义参数映射的核心步骤简介

a. 创建springboot 的基础工程。
b. 增加配置文件,并根据业务需求自定义配置中的参数值。
c. 创建自定义参数对应的实体类。
d. 利用springboot的启动类,进行测试参数的获取。

(3) 自定义参数映射的步骤代码实现如下:

a. 基于maven创建一个工程。
b.修改pom.xml 文件,核心内容如下:

   <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
  
  <dependencies>

  <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
             <version>1.4.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>1.4.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

  </dependencies>

c. 增加配置文件,并根据业务需求自定义配置文件中参数值。
在工程的resources目录下创建配置文件application.properties,里边内容如下:

other.name=helloword
other.email=helloword@163.com

d. 创建自定义参数对应的实体类

@Component
@ConfigurationProperties(prefix = "other")
public class OtherProperties {

    private String name;
    
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
     @Override
        public String toString() {
            return "OtherProperties{" +
                    "name='" + name + '\'' +
                    ", email='" + email + '\'' +
                    '}';
        }
}

e. 利用springboot的启动类,进行测试参数的获取

//springboot的应用标识
@SpringBootApplication
public class BootApplication implements CommandLineRunner {
     
     @Autowired
     private OtherProperties otherProperties;

     public static void main(String[] args) {
          //程序启动入口,启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
          SpringApplication.run(BootApplication.class, args);
     }

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("\n" + otherProperties.toString());
        System.out.println();

    }
}

2. SpringBoot开发及线上等多环境设置

在实际项目中,可能存在研发人员的开发环境和线上参数配置不一致的情况,为了让研发人员能灵活的在多种情况下切换配置参数,springboot提供了一种多环境配置的方式,具体讲解如下:

(1) springboot将配置文件分成1+N个文件

1是指:主配置文件,里边核心定义选取N中的哪个文件,命名为:application.yml,里边的核心内容如下:

N个文件的取名规则是:application-xxx.yml,比如我们这去两个文件,名称分别为:

application-dev.yml

## 系统属性
system:
  internalTime: ${random.long}
  machineId: ${random.uuid}
  database: mysql
  sumup: 机器的标识:${system.machineId},用的数据库是:${system.database}

application-prod.yml

## 系统属性
system:
  internalTime: ${random.long}
  machineId: ${random.uuid}
  database: oracle
  sumup: 机器的标识:${system.machineId},用的数据库是:${system.database}

(3) application.yml文件中active这个属性写什么,决定了项目中实际应用哪个文件,比如这里我们写dev,就是项目中会使用application-dev.yml中的文件内容

## 配置是生产环境还是开发环境
spring:
  profiles:
    active: dev

(3) 基于配置文件,编写相应的参数映射实体类,用来获取参数中的值

@ConfigurationProperties(prefix = "system")
public class SystemProperties {
    
    private long internalTime;
    private String machineId;
    private String database;
    private String sumup;

    public long getInternalTime() {
        return internalTime;
    }

    public void setInternalTime(long internalTime) {
        this.internalTime = internalTime;
    }

    public String getMachineId() {
        return machineId;
    }

    public void setMachineId(String machineId) {
        this.machineId = machineId;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getSumup() {
        return sumup;
    }

    public void setSumup(String sumup) {
        this.sumup = sumup;
    }
    
    @Override
    public String toString() {
        return "SystemProperties{" +
                "internalTime='" + internalTime + '\'' +
                ", machineId='" + machineId + '\'' +
                ", database='" + database + '\'' +
                 ", sumup='" + sumup + '\'' +
                '}';
    }
}

(4) 在springboot的启动类中进行测试,启动类修改为如下内容:

//springboot的应用标识
@SpringBootApplication
public class BootApplication implements CommandLineRunner {

     @Autowired
     private SystemProperties systemProperties;

     public static void main(String[] args) {
          //程序启动入口,启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
          SpringApplication.run(BootApplication.class, args);
     }

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("\n" + systemProperties.toString());
        System.out.println();

    }
}

参考链接:https://www.toutiao.com/i6564140520291959300/

相关文章

网友评论

      本文标题:SpringBoot 参数映射,开发及线上设置

      本文链接:https://www.haomeiwen.com/subject/ndkjpftx.html