springboot的生产环境、测试环境的配置

之前的时候没有用到生产环境等等,遇到的问题是当我需要修改我的代码里面的变量的时候,就需要全盘地去修改,想到在application.properties中写入全局变量去做,但是这样的话需要其他的conponent注解到类上去

indexPath=D:/lucene_index/happy6

@Component
public class ConstantInPro {

    @Value("${indexPath}")
    String Path;

    public String getPath() {
        return Path;
    }
}

这样是有些麻烦的,后面问到教授,提出了不同环境的生产,主要的结构需要
在这里插入图片描述

在这里插入图片描述

然后我们需要在config中写一下这个类:

package com.etc.newmoudle.Config;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * author:langlang
 * data: 2020-01-08 10:11
 */

@Configuration

public class SysEnv implements EnvironmentAware {

    /**
     * 项目环境
     */
    public static String active;
    /**
     * 启动端口
     */
    public static String port;
    /**
     * 项目上下文路径
     */
//    public static String contextPath;

    @Override
    public void setEnvironment(Environment environment) {
        active = environment.getProperty("spring.profiles.active");
//        port = environment.getProperty("server.port");
//        contextPath = environment.getProperty("server.servlet.context-path");
    }
}
发布了74 篇原创文章 · 获赞 2 · 访问量 6466

猜你喜欢

转载自blog.csdn.net/weixin_42067668/article/details/103905909