(1)首先需要使用ApplicationArguments
package com.helloworld.helloworld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
// 通过ApplicationArguments传递main方法参数
@Autowired
private ApplicationArguments applicationArguments;
@RequestMapping("/hello")
public String hello(){
System.out.println(applicationArguments.getNonOptionArgs());
return "hello springboot";
}
}
package com.helloworld.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Arrays;
@SpringBootApplication
public class HelloworldApplication {
public static void main(String[] args) {
System.out.println(Arrays.toString(args));
SpringApplication.run(HelloworldApplication.class, args);
}
}
(2)使用jar包方式启动
当没有任何参数时
当存在参数时
(3)使用application.properties
创建application.properties
强行修改application.properties名字
总结: