方法一:可以通过实现EmbeddedServletContainerCustomizer接口来实现,代码:
package com.springboot1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @SpringBootApplication public class Springboot1Application implements EmbeddedServletContainerCustomizer { public static void main(String[] args) { SpringApplication.run(Springboot1Application.class, args); } @Override public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) { configurableEmbeddedServletContainer.setPort(8011); } }
方法二:更加简单在src/resource下增加文件application.properties加入server.port=8081即可