SpringMVC conflicts with existing, non-compatible bean definition of same name and class solution, springmvc obtains bean

Source: Internet
Author: User

SpringMVC conflicts with existing, non-compatible bean definition of same name and class solution, springmvc obtains bean
Cause

Recently, a colleague from the project team encountered a problem. The module he was responsible for, SpringMVC Controller, and the Controller class names of other modules were duplicated, causing the whole project to fail.

The following error is reported in the background:

××Controller' for bean class [××ontroller] conflicts with existing, non-compatible bean definition of same name and class 

At lunch, he complained about the problem with me and said he could not find a solution.

Later I Thought About It. SpringMVC's Controller should adopt a key/value-like ing method. By default, the class name (not full class name) of cotroller is used as the key. In this way, if the two Contoller names under different packages are duplicated, the keys in controller map in SpringMVC container management will be repeated.

Solving this problem is also relatively simple.

In @ Controller, use the duplicate name.

For 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";    }}

Although the above two codes are under different packages, that is, the full class names are different, but the class names are the same.

In this way, an error will be reported in the background when Tomcat is started:

SEVERE: Context initialization failedorg.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]
Cause:

Because if the annotation @ Controller is used, SpringMVC writes the first letter of the class name in lower case by default and puts it in a map.

For example, in the above example, although the names of the two classes are different, they do not use the name when using the @ Controller annotation. When SpringMVC scans the Controller, it resolves all of them to billSaveController by default. Then, it uses the billSaveController as the key and places it in a global map.

In this way, two controllers with identical keys will appear. SpringMVC does not use the overwriting method to process controllers with different full-class names with the same key, and the preceding errors will occur during scanning.

Solution:

Use the name on @ Controller

For 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("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";    }}

In the above two controllers, you only need to ensure that one is named, but it is best to use both.

This is a good programming method, because you cannot guarantee that others will not use Controller with the same class name as you.

Postscript:

In the afternoon, I asked my colleagues to try it.

 

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.