错误详情
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'BookContorller' method
com.contorller.BookContorller#list(Model)
to { [/books/listAll]}: There is already 'bookContorller' bean method
com.contorller.BookContorller#list(Model) mapped.
提示原因指出:Controller中出现重复url,但是代码中没有重复
错误原因
xml文件中配置的bean id 为大写开头
根本原因
当@Controller未指定value值时,默认创建bean id 为小写开头的类名
然而又在xml文件中显示配置了大写开头id的bean,导致出现有相同url的bean
一个为bookContorller,注解所创建的
一个为BookContorller,xml文件显示创建
解决方法
将@Controller创建的bean的id与xml文件中指定的id改为相同
建议
在spring中配置Controller时,不要在一个bean中同时使用注解和xml显示配置,不然容易触发上述问题