步骤:
- 添加依赖
- 修改配置
- 编写代码
详细信息如下:
- 添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
- 修改配置
# 应用服务 WEB 访问端口
server.port=8080
server.servlet.context-path=/
- 编写代码
package io.ijunfu.blog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
@SpringBootApplication
public class BlogBackendApplication {
private static int port;
private static String contentPath;
public static void main(String[] args) {
SpringApplication.run(BlogBackendApplication.class, args);
log.info("App running at:");
log.info("- Local: http://localhost:{}{}", port, contentPath);
try {
String ip = InetAddress.getLocalHost().getHostAddress();
log.info("- Network: http://{}:{}{}", ip, port, contentPath);
} catch (UnknownHostException e) {
log.error("get local host fail: {}", e.getMessage());
}
}
@Value("${server.port}")
public void setPort(int port) {
BlogBackendApplication.port = port;
}
@Value("${server.servlet.context-path}")
public void setContentPath(String contentPath) {
BlogBackendApplication.contentPath = contentPath;
}
}