Java 启动参数 -- 和 -D写法的区别

当我们配置启动1个java 项目通常需要带一些参数

例如 -Denv = uat , --spring.profiles.active=dev 这些

那么用-D 和 – 的写法区别是什么?



双横线写法

其中这种写法基本上是spring 和 spring 框架独有

最常用的无非是就是上面提到的 --spring.profiles.active=dev 啦
这种写法必须写在 java xxx.jar 的后面

例如:

java xxx.jar --spring.profiles.active=dev

如果我们想自己在java 程序里自定义1个custom的 用-- 指定的参数
可以参考下面代码

@SpringBootApplication
@PropertySource("classpath:query.properties")
@Slf4j
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ApplicationRunner applicationRunner() {
        return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
                log.info("customParam: " + args.getOptionValues("customParam"));
            }
        };
    }

如果需要在IDEA的run configuration 配置, 则需要加在 CLI Arguments 那个field里
如下:
在这里插入图片描述

横线D写法 -D

这种写法通常是配置系统和VM的参数
最常用的无非是 配置代理
-D参数 必须写在java 和 xxx.jar 的中间
例如:

java -Dhttp.proxyHost=xxxx -Dhttp.proxyPort=7890 xxx.jar --spring.profiles.active=dev

这种方法配置的参数可以在代码中用System.getProperty()获取
当然在代码里用System.setProperty() 设置也一样的
例如:

@Component
@Slf4j
@EnableAsync
public class MyInitializer {

    @PostConstruct
    public void init() {
        log.info("Application started...");
        //setProxy();
        printProxy();
    }

    private void setProxy() {
        System.setProperty("https.proxyHost", "10.0.1.223");
        System.setProperty("https.proxyPort", "7890");
    }

    private void printProxy(){
       log.info("https.proxyHost: " + System.getProperty("https.proxyHost"));
       log.info("https.proxyPort: " + System.getProperty("https.proxyPort"));
    }

在IDEA里, 则需要把这种参数写在 VM参数的field里
如下:
在这里插入图片描述

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值