No qualifying bean of type [xxx] is defined

本文记录了一次使用Spring框架时遇到的NoSuchBeanDefinitionException异常,并详细解释了解决过程。问题出现在尝试从Spring上下文中获取未被正确定义的SyncPhoneController实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

No qualifying bean of type [org.wz.controller.SyncPhoneController] is defined

写了一个清洗数据的接口

项目:

spring配置:

	<!-- 扫描注解 -->
		<mvc:annotation-driven />
		<context:component-scan base-package="org.wz" />
    
	

 所有代码写完后,执行 main方法

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:*.xml");
		SyncPhoneController syncPhoneController= ctx.getBean(SyncPhoneController.class);
		syncPhoneController.syscPhoneBypage();
	}

抛出异常

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.wz.controller.SyncPhoneController] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:372)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1063)
    at org.wz.controller.SyncPhoneController.main(SyncPhoneController.java:47)
 

解决办法:

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:config/application-context.xml");
		SyncPhoneController syncPhoneController= ctx.getBean(SyncPhoneController.class);
		syncPhoneController.syscPhoneBypage();
	}

把  "classpath:*xml"   改成"classpath:config/application-context.xml"

ok 了

一点点改变,有很大的不同。

 

若依微服务:启动类调整@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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值