zoukankan      html  css  js  c++  java
  • SpringBoot(三):springboot启动参数

     springboot默认启动入口函数是支持接收参数,并且在整个应用程序内部也可以获取到这些参数,并且如果传递的参数是一些内部定义的参数将会被映射到springboot内部配置项,从而达到配置效果。

    springboot入口参数传递与获取:

    方式1)springboot 配置项目启动传递参数:

    a)在idea导航Run->Edit Configuration...

    b)Edit Configuration...下设置启动参数:

    c)修改SpringBoot启动入口函数:

    package app;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    
    import java.util.Arrays;
    
    @ComponentScan("com.dx.controller")
    @EnableAutoConfiguration
    public class App {
        public static void main(String[] args) {
            System.out.println(Arrays.toString(args));
            SpringApplication.run(App.class, args);
        }
    }

    d)在HelloWordController.java类中通过ApplicationArguments获取入口参数:

    package com.dx.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class HelloWordController {
        @Autowired
        private ApplicationArguments applicationArguments;
    
        @RequestMapping(value = "/index", method = RequestMethod.GET)
        public String index() {
            System.out.println(applicationArguments.getNonOptionArgs());
            System.out.println("index is running...");
            return "welcome";
        }
    }

    e)启动时,参看打印信息:

    f)在浏览器中访问http://localhost:8888/index,查看打印输出信息:

    方式2)springboot jar包运行时传递参数:

    a)通过maven打包项目为jar包;

    b)cmd中运行java -jar xxx.jar username=root password=pwd;

    c)查看启动信息;

    d)浏览器访问http://localhsot:8888/index,查看cmd屏幕输出信息。

    springboot传递系统内部定义配置参数:

    1)修改上述参数"username=root password=pwd"为"spring.config.name=application888 username=root password=pwd"

    2)此时在src下、src esources、src esourcesconfig其中任意目录下添加application888.properties 文件,并修改内容为:

    server.port=8880

    ,此时启动项目,将会发现启动端口已经发生变更,变更为8880端口。

  • 相关阅读:
    直接插入排序
    直接选择排序
    冒泡排序
    归并排序
    进程调度
    进程与线程
    c语言struct和c++struct的区别
    二叉搜索树、AVL平衡二叉搜索树、红黑树、多路查找树

    6-11 先序输出叶结点
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/8734486.html
Copyright © 2011-2022 走看看