• (B)springboot配置开发和测试环境并添加启动路径


    嗯,开发和测试环境要分离,这是一般共识(虽然我工作过的公司都没有这种分离),spring boot也可以按照配置文件的读取来做到这一点。

    上图有三个application开头的配置文件,要达到能够读取外部配置文件的目的,我们需要引入一个依赖包,就是红色箭头所指的那个,把这段加入pom.xml的<dependencies></dependencies>节点里面

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency> 

    三个application文件有什么不同呢?其实没什么不同,就是名字不一样啦

     命名不一样,我在里面配置了不同端口,用于区分,那么实际读取的application.properties是通过

    #当前服务环境为开发测试环境
    spring.profiles.active=dev
    #spring.profiles.active=prod

    去读取开发或者生产的环境的。

    第一行还可以配置我们项目的根访问路径,比如没有加之前我们在浏览器是按localhost:9090/hello访问的,加了/wow之后,就是按照localhost:9090/wow/hello访问。

    开发环境和测试环境配置好之后,编写一个测试类controller

    @RestController
    public class UserController {
        @RequestMapping("/getUser")
        public User getUser() {
            User user= new User();
            user.setId(PlantUuid.moreUuid(1));
            user.setName("ahei");
            user.setPwd("123456");
            return user;
        }
    }

    其中的工具类PlantUuid如下:

    package com.example.util;
    
    import java.util.UUID;
    
    public class PlantUuid {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //moreUuid(10);
        }
    
        //得到32位的uuid
        public static  String createUuid32() {
            String uuid = "";
            uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            System.out.print("---生成的uuid是---"+uuid+"
    ");
            return uuid;
        }
        
    
        //一次生成多个uuid
        public static  String  moreUuid(Integer t) {
            String more_uuid = "";
            Integer k = 0;
            for (k=0;k<t;k++) {
                more_uuid = createUuid32();
            }
            return more_uuid;
        }
    }

    在浏览器按localhost:9090/wow/getUser访问

    切换端口也可以的哦!

  • 相关阅读:
    git学习笔记
    ExtJs自学教程(1):一切从API開始
    Floodlight 处理交换机增加/移除过程
    飘逸的python
    Mapreduce运行过程分析(基于Hadoop2.4)——(三)
    oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
    Linux学习笔记总结
    看完锤子手机公布会直播 有感
    iOS iOS8中 问题&quot;registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later&quot; 解决方式
    读书笔记-HBase in Action-第二部分Advanced concepts-(3)非Javaclient
  • 原文地址:https://www.cnblogs.com/tulpen/p/9803229.html
Copyright © 2020-2023  润新知