如何解决 Spring Boot 中的此错误?
我正在尝试获取端口号和主机名,但我不断收到此错误。
@SpringBootApplication
public class RegistrationFormApplication implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@Autowired
Environment environment;
public static void main(String[] args) {
SpringApplication.run(RegistrationFormApplication.class, args);
}
Logger logger;
@Override
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent embeddedServletContainerInitializedEvent) {
logger.info("Port " + embeddedServletContainerInitializedEvent.getApplicationContext().getEmbeddedServletContainer().getPort());
try {
logger.info("HOST Address " + InetAddress.getLocalHost().getHostAddress());
logger.info("Host Name " + InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException e) {
}
}
}
最佳答案
可能是因为您在使用 Spring Boot 2.x 时复制/粘贴了一些为 Spring Boot 1.x 编写的代码,其中该类已被删除。
尝试使用ServletWebServerInitializedEvent
和port = event.getWebServer().getPort()
。
参见https://self-learning-java-tutorial.blogspot.com/2018/07/spring-boot-get-port-of-spring-boot.html了解获取服务器端口的不同方法。
关于java - 为什么我收到此错误 "EmbeddedServletContainerInitializedEvent cannot be resolved to a type"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56982909/