NoSuchBeanDefinitionException: No bean named 'xxx' is defined

一开始学习spring这种问题恐怕十分常见了,遇见这样的问题基本就是以下几种情况

1. 缺少包或者包冲突

我把我用到的包贴出来大家可以对照一下
//spring基本依赖
compile "org.springframework:spring-context:4.3.9.RELEASE"
compile "org.springframework:spring-beans:4.3.9.RELEASE"
compile "org.springframework:spring-core:4.3.9.RELEASE"
compile "org.springframework:spring-expression:4.3.9.RELEASE"
//spring的aop操作依赖
compile "org.springframework:spring-aspects:4.3.9.RELEASE"
compile "org.springframework:spring-aop:4.3.9.RELEASE"
compile "aspectj:aspectjweaver:1.5.4"
compile "aopalliance:aopalliance:1.0"
//spring的jdbc操作依赖
compile "mysql:mysql-connector-java:5.1.38"
compile "org.springframework:spring-jdbc:4.3.9.RELEASE"
compile "org.springframework:spring-tx:4.3.9.RELEASE"
compile "com.mchange:c3p0:0.9.5.2"
//springMVC的依赖
compile "org.springframework:spring-webmvc:4.3.9.RELEASE"
compile "org.springframework:spring-web:4.3.9.RELEASE"
compile "javax.servlet:jstl:1.2"
compile "javax.servlet:servlet-api:2.5"
compile "javax.servlet.jsp:jsp-api:2.2"

2. bean是否未定义或名字写错

仔细检查自己的bean的名字有没有写错,如果用@Autowired的话,还要检查类型是否匹配。
还要检查@Repository等有没有忘了配置

3. spring的配置xml的路径是否正确

在resource下需要用classpath:开头(如果不行可以试试classpath*:),在webapp下需要WEB_INF/xxx
还要注意,你的路径应该对应的是tomcat下的路径!

4. web.xml配置问题(这也是我这次出的问题)

如果你用单元测试(junit)可以通过,但是开了tomcat就报错的话,基本都是web.xml配置问题
我当时已经把spring的xml配置进去了,也就是这样,但还是报错
<context-param>
        <!--名字必须是这个,路径要仔细检查有没有错-->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml,classpath:spring/spring_mybatis.xml</param-value>
    </context-param>
最后才发现我的监听器没有加
<!--spring监听器,加载spring文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
顺带提一下,前端控制器的init-param最好要配一下,不然他会自动寻找xxx-servlet.xml加载,这里也有可能出错
 <!--springmvc前段控制器配置-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置springmvc加载的配置文件(处理器、映射器、适配器等)-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/dispatcher-servlet.xml</param-value>
        </init-param>
    </servlet>

5. 天下之大无奇不有,如果还有其他奇葩的错的,还恕我无能为力了

听说各个版本还会有一些不同的问题,这些就很难办了(主要是路径的问题,可以试试多用上点*,碰上的几率会大一点…)
若依微服务:启动类调整@SpringBootApplication(exclude = {DynamicDataSourceAutoConfiguration.class}) 配置文件 # 分库分表配置 shardingsphere: datasource: names: master,slave master: driver-class-name: com.mysql.cj.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource username: root 启动报错 11:34:53.889 [main] ERROR o.s.b.w.e.t.TomcatStarter - [onStartup,61] - Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'webMvcMetricsFilter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'webMvcMetricsFilter' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMeterRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourcePoolMetadataMeterBinder' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration$DataSourcePoolMetadataMetricsConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourcePoolMetadataMeterBinder' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceConfiguration': Unsatisfied dependency expressed through field 'properties'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
最新发布
08-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值