SPRINGMVC conflicts with existing, non-compatible bean definition of same name and class solution, Sprin__java

Source: Internet
Author: User
Http://www.bkjia.com/Javabc/1007408.html

SPRINGMVC conflicts with existing, non-compatible bean definition of same name and class solution, Springmvc get Bean
problem causes

Recently, the project team colleagues encountered a problem, his own responsible for the module, SPRINGMVC controller and other modules of the Controller class name, resulting in the whole project is not up.

The error in the background report is this:

Xxcontroller ' for Bean class [Xxontroller] conflicts with existing, non-compatible bean definition of same name and class 

At lunch, he complained to me about the problem and said he couldn't find a way.

After I thought about it, SPRINGMVC's controller should be handled in a way that resembles a key-value pair (key/value) mapping. The key, by default, is to use the Cotroller class name (not the full class name) as the key. In this way, if the names of the two contoller under different packages are duplicated, the key in the controller map in the SPRINGMVC container management will repeat.

The solution to this problem is also relatively simple.

In @controller, it's OK to use the name of the duplicate.

The following example:

Test.controller.bill.BillSaveController
Package Test.controller.bill;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Liuch on 5/27/15.
 *
/@Controller @RequestMapping ("/billsave") public
class Billsavecontroller {

    @RequestMapping ("/ Dosave ") Public
    String Savebill () {return

        " Billsave ";
    }

}

and Test.controller.bill.BillSaveController

Package test.controller.billsave;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Liuch on 5/27/15.
 * *
@Controller
@RequestMapping ("/billsave_test") public
class Billsavecontroller {

    @ Requestmapping ("/test") public
    String test () {return
        "Test";
    }

}

The above two codes, although under different packages, that is, the whole class name is different, but the class name is the same.

In this way, when Tomcat starts, the background will have an error:

Severe:context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: 

Unexpected exception parsing XML document from ServletContext resource
[/web-inf/dispatcher-servlet.xml]; 

Nested exception is java.lang.IllegalStateException: 

annotation-specified bean name ' Billsavecontroller ' 
for Bean class [Test.controller.billsave.BillSaveController] 
conflicts with existing, non-compatible bean definition of same name and 
class [Test.controller.bill.BillSaveController]
problem Reason:

Because if you do not use a name when using annotations @Controller, SPRINGMVC defaults The first letter of the class name to lowercase and then places it in a map.

For example, the above example, although the above two classes full class name is different, but they use the @controller annotation, did not use the name. When SPRINGMVC scans controller, they all default to Billsavecontroller. Then take this billsavecontroller as the key, and put it in a global map.

In this way, there will be two keys exactly the same controller. Because SPRINGMVC does not use overlay to handle different full class name controller with the same key, the error is wrapped on the scan. Solutions:

Using names on @controller

such as: Test.controller.bill.BillSaveController in

Package Test.controller.bill;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Liuch on 5/27/15.
 */
@Controller ("Testbillsave")
@RequestMapping ("/billsave") public
class Billsavecontroller {

    @ Requestmapping ("/dosave") public
    String Savebill () {return

        "Billsave";
    }

}
In Test.controller.billsave.BillSaveController, use:
Package test.controller.billsave;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Created by Liuch on 5/27/15.
 */
@Controller ("Realbillsave")
@RequestMapping ("/billsave_test") public
class Billsavecontroller {

    @RequestMapping ("/test") public
    String test () {return
        "Test";
    }

}

Of the top two controller, just make sure that one is named, but it's best to use two.

This is a good way to program, because you cannot guarantee that others will not use the same class name as you controller. PostScript:

Let colleagues try this afternoon, sure enough.


I encountered one of the same problems in the project:

The error is as follows:
caused by:org.springframework.beans.factory.BeanDefinitionStoreException:Unexpected exception parsing XML document From class path resource [Springconf/servlet-context.xml]; Nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: annotation-specified Bean name ' checkweakpasswordjob ' For Bean class [ COM.SIEMENS.CT.ITS.IAM.WEAKPASSWORD.CHECKWEAKPASSWORDJOB] conflicts with existing, non-compatible bean definition of same name and class [Com.siemens.ct.its.iam.weakpassword.audit.task.CheckWeakpasswordJob]
At Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions ( xmlbeandefinitionreader.java:414)
At Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions ( xmlbeandefinitionreader.java:336)
At Org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions ( xmlbeandefinitionreader.java:304)
At Org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource ( defaultbeandefinitiondocumentreader.java:243)
... More
caused by:org.springframework.context.annotation.conflictingbeandefinitionexception:annotation-specified bean Name ' Checkweakpasswordjob ' for Bean class [Com.siemens.ct.its.iam.weakpassword.CheckWeakpasswordJob] conflicts with Existing, non-compatible bean definition of same name and class [ COM.SIEMENS.CT.ITS.IAM.WEAKPASSWORD.AUDIT.TASK.CHECKWEAKPASSWORDJOB]
At Org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate ( classpathbeandefinitionscanner.java:345)
At Org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan ( classpathbeandefinitionscanner.java:283)
At Org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse ( componentscanbeandefinitionparser.java:87)
At Org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse (namespacehandlersupport.java:74)
The result is similar to the above method of processing successfully.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.