关于springframework的报错问题,UnsatisfiedDependencyException

目录


问题

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name “xxx”

本人最近在整合Spring+SpringMVC+Mybatis框架遇到一个问题,网上也都出现过这个问题,但是都不能解决我的问题,bug提示如下,终于知道八阿哥为啥这么讨厌了:
**org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name** 'pageController': Unsatisfied dependency expressed through field 'contentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contentServiceImpl': Unsatisfied dependency expressed through field 'contentMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.net.shop.mapper.ContentMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)


解决

  • 将出错部分的代码注释直到没有报错为止,从源头处理问题;
    这里写图片描述

  • 从提示可以看出问题在于自动注入发生问题;
    这里写图片描述

这里写图片描述

  • 首先要看是否将@Service,@Autowired等注解写上以及拼写;

  • 其次要看清楚是否将接口注入,而不是实现类,不是Impl结尾的;
    这里写图片描述

  • 最后再看是否是框架的问题,mapper的扫描是否发生问题,注意红框内的部分,是否与你的dao/mapper层一致。
    这里写图片描述

根据提供的引用内容,nacos启动报错ERROR Startup errors : org.springframework.beans.factory.UnsatisfiedDependencyException,这个错误通常是由于依赖注入问题引起的。具体来说,可能是某个bean的依赖项无法满足,或者依赖项没有正确注入。解决此问题的方法如下: 1.检查nacos配置文件是否正确,特别是数据库连接信息是否正确。 2.检查nacos依赖项是否正确,特别是版本是否匹配。 3.检查nacos启动日志,查看哪个bean无法满足依赖项,然后检查该bean的依赖项是否正确注入。 4.如果无法确定问题所在,可以尝试使用调试器进行调试,以查找问题。 下面是一个可能的解决方案: ```java org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘xxxController’: Unsatisfied dependency expressed through field ‘xxxService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘xxxServiceImpl’: Unsatisfied dependency expressed through field ‘xxxRepository’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘xxxRepository’: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.xxx.xxx.xxx ``` 根据上述错误信息,我们可以看到,xxxRepository这个bean的初始化方法失败了,原因是它不是一个受管理的类型。这通常是由于没有在应用程序上下文中注册该类型引起的。解决此问题的方法是在应用程序上下文中注册该类型,例如: ```java @Configuration @EnableJpaRepositories(basePackages = "com.xxx.xxx.xxx") @EntityScan(basePackages = "com.xxx.xxx.xxx") public class AppConfig { // ... } ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值