注意:修改@SpringBootConfiguration注解修饰的类中的启动方法即可。
一、方式一:(使用SpringApplication对象)
SpringApplication springApplication=new SpringApplication(MicroservicesProjectApplication.class);
Map<String,Object> properties=new LinkedHashMap<>();
properties.put("server-port",0);
springApplication.setDefaultProperties(properties);
springApplication.run(args);
二、方式二:(使用SpringApplicationBuilder对象)
特点:使用Fluent API(流利的API)
new SpringApplicationBuilder(MicroservicesProjectApplication.class)
//设置端口地址
//单元测试时 PORT=RANDOM 随机端口
.properties("server.port=0") //随机向操作系统(OS)要可用端口
.run(args);