Annotation-specified bean name conflicts with existing, non-compatible bean def

后端 未结 13 812
再見小時候
再見小時候 2021-01-31 01:51

I\'m having a problem with some Spring bean definitions. I have a couple of context xml files that are being loaded by my main() method, and both of them contain almost exclusiv

相关标签:
13条回答
  • 2021-01-31 01:52

    Scenario:

    I am working on a multi-module Gradle project.

    Modules are:

    - core, 
    - service,
    - geo,
    - report,
    - util and
    - some other modules.
    

    So primarily we have prepared a Component[locationRecommendHttpClientBuilder] in geo module.

    Java Code:

    import org.springframework.stereotype.Component
    
    @Component("locationRecommendHttpClientBuilder")
    class LocationRecommendHttpClientBuilder extends PanaromaHttpClientBuilder {
        @Override
        PanaromaHttpClient buildFromConfiguration() {
            this.setURL(PanaromaConf.getInstance().getString("locationrecommend.url"))
            this.setMethod(PanaromaConf.getInstance().getString("locationrecommend.method"))
            this.setProxyHost(PanaromaConf.getInstance().getString("locationrecommend.proxy.host"))
            this.setProxyPort(PanaromaConf.getInstance().getInt("locationrecommend.proxy.port", 0))
            return super.build()
        }
    }
    

    application-context.xml

    <bean id="locationRecommendHttpClient"
          class="au.co.google.panaroma.platform.logic.impl.PanaromaHttpClient"
          scope="singleton" factory-bean="locationRecommendHttpClientBuilder"
          factory-method="buildFromConfiguration" />
    

    Then it is decided to add this component in core module.

    One engineer has previous code for geo module and then he has taken the latest module of core but he forgot to take the latest geo module.

    So the component[locationRecommendHttpClientBuilder] is double times in his project and he was getting the following error.

    Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'LocationRecommendHttpClientBuilder' for bean class [au.co.google.app.locationrecommendation.builder.LocationRecommendHttpClientBuilder] conflicts with existing, non-compatible bean definition of same name and class [au.co.google.panaroma.platform.logic.impl.locationRecommendHttpClientBuilder]

    Solution Procedure:

    After removal the component from geo module, component[locationRecommendHttpClientBuilder] is only available in core module. So there is no conflicting situation. Issue is solved by this way.

    0 讨论(0)
  • 2021-01-31 01:56

    I had a similar problem, with two jar libraries (app1 and app2) in one project. The bean "BeanName" is defined in app1 and is extended in app2 and the bean redefined with the same name.

    In app1:

    package com.foo.app1.pkg1;
    
    @Component("BeanName")
    public class Class1 { ... }
    

    In app2:

    package com.foo.app2.pkg2;
    
    @Component("BeanName")
    public class Class2 extends Class1 { ... }
    

    This causes the ConflictingBeanDefinitionException exception in the loading of the applicationContext due to the same component bean name.

    To solve this problem, in the Spring configuration file applicationContext.xml:

        <context:component-scan base-package="com.foo.app2.pkg2"/>
        <context:component-scan base-package="com.foo.app1.pkg1">
            <context:exclude-filter type="assignable" expression="com.foo.app1.pkg1.Class1"/>
        </context:component-scan>
    

    So the Class1 is excluded to be automatically component-scanned and assigned to a bean, avoiding the name conflict.

    0 讨论(0)
  • 2021-01-31 01:58

    I also had a similar problem. I built the project again and the issue was resolved.

    The reason is, there are already defined sequences for the Annotation-specified bean names, in a file. When we do a change on that bean name and try to run the application Spring cannot identify which one to pick. That is why it shows this error.

    In my case, I removed the previous bean class from the project and added the same bean name to a new bean class. So Spring has the previous definition for the removed bean class in a file and that conflicts with the newly added class while compiling. So if you do a 'build clean', previous definitions for bean classes will be removed and compilation will success.

    0 讨论(0)
  • 2021-01-31 02:00

    I had a similar problem, and it was because one of my beans had been moved to another directory recently. I needed to do a "build clean" by deleting the build/classes/java directory and the problem went away. (The error message had the two different file paths conflicting with each other, although I knew one should not actually exist anymore.)

    0 讨论(0)
  • 2021-01-31 02:01

    Explanation internal working on this error

    You are getting this error because after instantiation the container is trying to assign same object to both classes as class name is same irrespective of different packages......thats why error says non compatible bean definition of same name ..

    Actually how it works internally is--->>>>.

    pkg test1; …. @RestController class Test{}

    pkg test2; …. @RestController class Test{}

    First container will get class Test and @RestController indicates it to instantiate as…test = new Test(); and it won’t instantiate twice After instantiating container will provide a reference variable test(same as class name) to both the classes and while it provide test reference To second class it gets non compatible bean definition of same name ……

    Solution—>>>>

    Assign a refrence name to both rest controller so that container won’t instantiate with default name and instantiate saperately for both classes irrespective Of same name

    For example——>>>

    pkg test1; …. @RestController(“test1”) class Test{}

    pkg test2; …. @RestController(“test2”) class Test{}

    Note:The same will work with @Controller,@Service,@Repository etc..

    Note: if you are creating reference variable at class level then you can also annotate it with @Qualifier("specific refrence name") for example @Autowired @Qualifier("test1") Test test;

    0 讨论(0)
  • 2021-01-31 02:01

    Refresh gradle project on Eclipse solved this problem for me

    0 讨论(0)
提交回复
热议问题