springboot启动报错@Bean definition illegally overridden by existing bean definition

在做学习springCloud集成Eureka时启动EurekaClient服务报如下错误:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'eurekaClient' defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.class]: @Bean definition illegally overridden by existing bean definition: Generic bean: class [com.init.springCloud.EurekaClient]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null

我的EurekaClient服务的启动如下:

@SpringBootApplication
@EnableEurekaClient
public class EurekaClient {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClient.class, args);
    }
}

在网上查找原因居然都没有。

没办法只能仔细查看错误信息,关注到@Bean definition illegally overridden by existing bean definition,意思是@bean定义被现有bean定义非法重写,可是我就写了一个启动类EurekaClient,原因只能是这个类有问题。字面上的意思bean被非法重写,那就是我的EurekaClient这个类在生成@bean的时候重新定义了某个@bean。抱着试试的心态将EurekaClient类名改为别的(EurekaClientApplication),运行成功!

想验证下原因,于是:查看springboot在启动的时候为我们注入了哪些bean

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
    
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
 
            System.out.println("Let's inspect the beans provided by Spring Boot:");
 
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
 
        };
    }

}
 

输出如下:

Let's inspect the beans provided by Spring Boot:
apacheHttpClientBuilder
apacheHttpClientFactory
applicationAvailability
applicationTaskExecutor
asyncLoadBalancerInterceptor
asyncRestTemplateCustomizer
basicErrorController
beanNameHandlerMapping
beanNameViewResolver
blockingLoadBalancerClientRibbonWarnLogger
bootstrapApplicationListener.BootstrapMarkerConfiguration
characterEncodingFilter

......

eurekaApplicationInfoManager
eurekaAutoServiceRegistration
eurekaClient
eurekaClientApplication
eurekaClientConfigBean
......

果然,在启动的时候springboot已经为我加载了eurekaClient,不能再用此命名。然后看到下面有eurekaClientApplication,会不会也是springboot自带的。于是我又将类EurekaClientApplication改为EurekaClientApplicationTest,变为输出eurekaClientApplicationTest了。到这里总算完美解决了~~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值