springboot production environment, the configuration of the test environment

When not used before production environment and so on, the problem encountered is when I need to modify my code inside the variable, you need to modify the overall, global variables do not think write in application.properties, but it in this case the need for other conponent notes to class up

indexPath=D:/lucene_index/happy6

@Component
public class ConstantInPro {

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

    public String getPath() {
        return Path;
    }
}

This is somewhat troublesome, back to the professor asked, proposed the production of different environments, the need for major structural
Here Insert Picture Description

Here Insert Picture Description

Then we need to write about this in the config class:

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");
    }
}
Published 74 original articles · won praise 2 · Views 6466

Guess you like

Origin blog.csdn.net/weixin_42067668/article/details/103905909