spring创建bean异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'sysUserController' method
com.benben.percms.system.controller.SysUserController#addOne(SysUser)
to {POST /addOne}: There is already 'sysRoleController' bean method
com.benben.percms.system.controller.SysRoleController#addOne(SysRole) mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$(AbstractBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:) ~[spring-context-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:) ~[spring-context-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:) ~[spring-boot-2.2..RELEASE.jar:2.2..RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:) [spring-boot-2.2..RELEASE.jar:2.2..RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:) [spring-boot-2.2..RELEASE.jar:2.2..RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-2.2..RELEASE.jar:2.2..RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-2.2..RELEASE.jar:2.2..RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:) [spring-boot-2.2..RELEASE.jar:2.2..RELEASE]
at com.benben.percms.PerCmsApplication.main(PerCmsApplication.java:) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:) ~[na:1.8.0_144]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:) [spring-boot-devtools-2.2..RELEASE.jar:2.2..RELEASE]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'sysUserController' method
com.benben.percms.system.controller.SysUserController#addOne(SysUser)
to {POST /addOne}: There is already 'sysRoleController' bean method
com.benben.percms.system.controller.SysRoleController#addOne(SysRole) mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:) ~[na:1.8.0_144]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.processCandidateBean(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:) ~[spring-webmvc-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:) ~[spring-beans-5.2..RELEASE.jar:5.2..RELEASE]
... common frames omitted
这个异常很简单,贴出来就是帮助一下新童鞋。
一般出现这个异常的时候是在controller出现的多一些,我就拿controller说吧。
我第一次看到这个异常的时候也很蒙蔽,我的代码明明没有错,为啥就是不能创建bean呢?
原来我是创建了两个名字不同但内容一样的controller 比如这样:

还有这个

根据上边的报错内容你一定会吧其中的某一个方法改名或者删掉,发现另一个方法又报错了,因为我这里是两组方法一样。
其实他不是因为spring有问题的,原因很简单,我们一个url链接应该对应一个接口 ,但是上边的代码却对应了两个,所以web不知道这个url应该到底对应谁,所以才会报错。
于是有两种解决方法:
1. 把两个类中的url链接都改成不一样的,但是应用大了之后免不了有很多功能一样的方法 就比如添加 一般我喜欢命名为add , 再改能改几个呀,要是人家有100个类都有这么个方法,你难道要起100个名字?
2.于是我们可以在类上边打个@RequestMapping(),给这个类定义一个类访问路径就好了,能少掉两个头发
spring创建bean异常的更多相关文章
- Spring创建Bean的顺序
一直对Spring创建bean的顺序很好奇,现在总算有时间写个代码测试一下.不想看过程的小伙伴可以直接看结论 目录结构: 其中:bean4.bean5包下的class没有注解@Component,测试 ...
- Spring 创建bean的模式
在默认情况下,spring创建bean是单例模式 scope="singleton ",还有一种方式为多例模式[prototype] scope sing ...
- spring创建bean的三种方式
spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...
- spring创建bean模式singleton与prototype的区别
spring 创建bean有单例模式(singleton)和原始模型模式(prototype)这两种模式. 在默认的情况下,Spring中创建的bean都是单例模式的(注意Spring的单例模式与Go ...
- spring 创建Bean最全实现方法
创建bean方式,spring创建bean的方式包含:自动注入方式和人工注入方式.分别为:1)xml 配置化方式 2)@bean注解注入方式3)@Component方式 4)接口注入方式 5)imp ...
- Spring 创建Bean的6种方式
前言 本文讲解了在Spring 应用中创建Bean的多种方式,包括自动创建,以及手动创建注入方式,实际开发中可以根据业务场景选择合适的方案. 方式1: 使用Spring XML方式配置,该方式用于在纯 ...
- Spring创建Bean的过程Debug
目录 Spring流程Debug 1.1 Spring测试环境搭建 1.2 Debug容器创建过程 1.3 AbstractApplicationContext的refresh()包含的13个方法分析 ...
- []Spring创建Bean的过程
1. beans包提供了以编程方式管理和操作bean的基本功能,而context包增加了ApplicationContext,它以一种更加面向框架的方式增强了BeanFactory的功能. 2. co ...
- spring创建bean及数据注入
通过spring的IoC可以实现由配置文件来创建类的对象,可以降低类鱼类之间的耦合, 通常我们都是在代码中控制对象的生成和属性注入,而使用IoC后,就可以将设计好的类交给IoC容器,让容器去控制对象的 ...
随机推荐
- dac FDMemTable
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- JNI传递修改自定义Java Class数组数据
声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 结合前面讲的2篇关于JNI的文章,这里直接把代码贴上,主要是要知道如果传递自定义Class Array的时 ...
- Vue.js——5.生命周期
Vue的生命周期 创建阶段new Vue1,beforeCreate() 表示在实例没有被创建出来之前会执行它加载data和methods2,caeated() data 和methods被初始化了 ...
- 挑战目标跟踪算法极限,SiamRPN系列算法解读
商汤科技智能视频团队首次开源其目标跟踪研究平台 PySOT.PySOT 包含了商汤科技 SiamRPN 系列算法,以及刚被 CVPR2019 收录为 Oral 的 SiamRPN++.此篇文章将解读目 ...
- Spring Bean的生命周期、Spring MVC的工作流程、IOC,AOP
1.Spring Bean的生命周期? (1)构造方法实例化bean. (2)构造方法设置对象属性. (3)是否实现aware接口,三种接口(BeanNameAware,BeanFactoryAwar ...
- 编程作业2.1:Logistic regression
题目 在这部分的练习中,你将建立一个逻辑回归模型来预测一个学生是否能进入大学.假设你是一所大学的行政管理人员,你想根据两门考试的结果,来决定每个申请人是否被录取.你有以前申请人的历史数据,可以将其用作 ...
- 图形化编程娱乐于教,Kittenblock实例,测试声音的响度
跟很多学生聊过,很多学生不是不努力,只是找不到感觉.有一点不可否认,同样在一个教室上课,同样是一个老师讲授,学习效果迥然不同.关键的问题在于,带入感,我能给出的建议,就是咬咬牙,坚持住,没有学不会的知 ...
- Openstack 使用Centos官方镜像创建实例记录
Openstack 使用Centos官方镜像创建实例记录 准备centos镜像 官方地址:http://cloud.centos.org/centos/7/images 可以看到有各种版本的镜像,我在 ...
- Linux分区挂载
Liunx采用树形的文件管理系统,也就是在Linux系统中,可以说已经没有分区的概念了.分区在Linux和其他设备一样都只是一个文件.要使用一个分区必须把它加载到文件系统中.这可能难于理解,继续往下看 ...
- iOS 中UITableView的深理解
例如下图:首先分析一下需求:1.根据模型的不同状态显示不同高度的cell,和cell的UI界面. 2.点击cell的取消按钮时,对应的cell首先要把取消按钮隐藏掉,然后改变cell的高度. 根据需求 ...