SpringBoot中的参数设置(三)

1参数的处理:在应用启动过程中,可以通过启动参数给应用传递一些额外的参数来控制应用的运行;

    1,在main方法中可以直接使用传入的参数;

    2,可以任何类中直接通过@Autowired注入一个ApplicationArguments对象;

@Controller
public class HelloController {
    @Autowired
    ApplicationArguments applicationArguments;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        //获取控制台参数
        System.out.println(applicationArguments.getNonOptionArgs());
        return "helloworld";
    }
}

 3,SpringBoot中主要的参数来源及优先级:

     1,命令行参数;

     2ServletConfigServletContext

     3,操作系统环境变量;

     4application-{profile}.properties或者YAML文件;

     5application.properties或者YAML文件;

 

4SpringBoot提供了方便的properties绑定机制;

    1)默认从application.properties中加载配置;

    2)资源文件(application.properties)加载顺序:

  1,当前目录/config子目录;

  2,当前目录;

  3classpath下得/config子目录;

  4classpath;

 

  可以通过(只能通过命令行参数配置--spring.config.name),

         如java -jar demo-1.1.1.jar --spring.config.name=app666(不用写.properties)

  spring.config.name配置配置文件名称

  spring.config.location配置配置文件具体加载地址

猜你喜欢

转载自blog.csdn.net/qq_37431224/article/details/103856761