Spring Boot Eureka Server启动失败:找不到ServerProperties Bean的解决方法
问题原因及解决方法
原因分析
- 打包方式与主类配置不兼容:项目设置为
war打包模式,但主类未继承SpringBootServletInitializer,导致Spring Boot的Web核心自动配置类(包括ServerProperties)无法被正确初始化加载。 - 冗余的依赖版本指定:在已通过
dependencyManagement导入Spring Cloud全局依赖管理的情况下,手动指定spring-cloud-starter-netflix-eureka-server的版本,可能引发依赖版本冲突,干扰自动配置Bean的创建流程。
解决方法
方法一:切换为Jar打包(推荐)
Eureka Server作为独立运行的注册中心,使用Jar打包是更合适的选择,无需额外Servlet适配:
- 修改pom.xml中的打包类型:
<packaging>jar</packaging>
- 保持现有主类代码不变,执行
mvn clean install后重新启动项目。
方法二:适配War打包的主类
若必须使用War打包,需调整主类继承SpringBootServletInitializer以支持Web容器部署:
@SpringBootApplication @EnableEurekaServer public class DiscoveryServerApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DiscoveryServerApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DiscoveryServerApplication.class); } }
方法三:移除冗余依赖版本指定
删除spring-cloud-starter-netflix-eureka-server的手动版本声明,让Spring Cloud的依赖管理自动匹配兼容版本:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <!-- 移除该行手动版本指定 --> <!-- <version>4.0.2</version> --> </dependency>
执行上述调整后,重新构建并启动项目,ServerProperties Bean将被正常加载,Eureka Server即可启动成功。
内容的提问来源于stack exchange,提问作者Thimira Pathirana
相关产品推荐
相关产品推荐

