1. application.yml
contextPath: /test
2. ParamUtil
@Component
public class ParamUtil {
public static String contextPath;
@Value("${contextPath}")
public void setContextPath(String contextPath) {
ParamUtil.contextPath = contextPath;
}
}
public class controller {
public void test() {
System.out.println(ParamUtil.contextPath);
}
}
@Component
public class ParamUtil {
@Value("${contextPath}")
public String contextPath;
}
public class controller {
@Autowire
private ParamUtil paramUtil;
public void test() {
System.out.println(paramUtil.contextPath);
}
}
3. 运行命令
java -jar test.jar
java -jar test.jar -DcontextPath=/usr/local/demo