How to solve the conflict of springboot- startup bean
Editor to share with you how to solve the problem of springboot- starting bean conflict. I hope you will get something after reading this article. Let's discuss it together.
Start bean conflict
Encountered the problem of bean conflict during a startup, indicating that there are two bean with duplicate names
Org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.test.api.Application]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'healthCheckController' for bean class [com.test.datahub.controller.HealthCheckController] conflicts with existing, non-compatible bean definition of same name and class [com.test.api.controller.HealthCheckController]
The project includes multiple modules, of which both An and B modules have the same class:
HealthCheckController, check the change information and find that I don't know who added the dependency of B module in A module, which caused this problem. Delete it and solve it.
Com.test B 1.0.0-SNAPSHOT startup prompts bean to repeat the question first
You only need to put a unique mark on the contextId attribute of the @ FeignClient annotation to solve the problem.
Principle
Because ClientConfiguration will be registered when registering feignClient. The reference code is as follows
Public void registerFeignClients (AnnotationMetadata metadata, BeanDefinitionRegistry registry) {/ /. Part of the code / / for (String basePackage: basePackages) {Set candidateComponents = scanner .findCandidateComponents (basePackage); for (BeanDefinition candidateComponent: candidateComponents) {if (candidateComponent instanceof AnnotatedBeanDefinition) {/ / verify annotated class is an interface / /. Omit part of the code / / this encapsulates all the attributes on the annotation on the map Map attributes = annotationMetadata. GetAnnotationAttributes (FeignClient.class.getCanonicalName ()); / / these two key methods see the following code block / / to get the name of the feignClient (focus on the method) String name = getClientName (attributes) / / this method is the step for spring to inject beanDefination (focus on this method) registerClientConfiguration (registry, name, attributes.get ("configuration"); registerFeignClient (registry, annotationMetadata, attributes);}
For the two key methods above, please see here
/ / @ param client the map is the private String getClientName (Map client) {if (client = = null) {return null;} / / if the name is obtained from contextId, then value has a value of String value = (String) client.get ("contextId"). / / if value has a value, then the following three if conditions will not go, so contextId can only ensure that bean does not repeat. / / if value has no value, value will be taken, because the serverName of multiple client is the same, then there will be a repeat bean if (! StringUtils.hasText (value)) {value = (String) client.get ("value") } if (! StringUtils.hasText (value)) {value = (String) client.get ("name");} if (! StringUtils.hasText (value)) {value = (String) client.get ("serviceId");} if (StringUtils.hasText (value)) {return value;} throw new IllegalStateException ("Either 'name' or' value' must be provided in @" + FeignClient.class.getSimpleName ()) } private void registerClientConfiguration (BeanDefinitionRegistry registry, Object name, Object configuration) {BeanDefinitionBuilder builder = BeanDefinitionBuilder .genericBeanDefinition (FeignClientSpecification.class); builder.addConstructorArgValue (name); builder.addConstructorArgValue (configuration) / / create beanDefinition at this location, but as you can see from the name format he created, the only change variable is name, and this name is the registry.registerBeanDefinition (name + "." + FeignClientSpecification.class.getSimpleName (), builder.getBeanDefinition ()) obtained by the method seen above. } after reading this article, I believe you have some understanding of "how to solve the problem of springboot- starting bean conflict". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!