当前位置: 首页 > 知识库问答 >
问题:

@AutoWired-找不到bean

岳阳文
2023-03-14

我的问题与@AutoWired类似-找不到依赖项类型的合格bean

我用的是Spring靴。类结构如下:

package myapp.services
  CalculationService
  CalculationServiceImpl
package myapp
  testcase

测试用例尝试@autowireCalculationServiceImplObjectMapper字段,但没有成功。我不确定我的配置有什么问题。由于服务的实现在测试用例的子包中,因此@componenscan不是必需的。服务类正确使用@service注释。

完整项目请访问:https://github.com/geoheil/spring

 java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
            Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

全堆栈跟踪

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:183)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:123)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'calculationController': Unsatisfied dependency expressed through constructor argument with index 0 of type [f10.innovation.services.CalculationService]: : No qualifying bean of type [f10.innovation.services.CalculationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [f10.innovation.services.CalculationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    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:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 29 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [f10.innovation.services.CalculationService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

共有1个答案

谷飞星
2023-03-14

我假设您的配置类遗漏了一些信息。您在@SpringApplicationConfiguration批注(CalculationController.class)中提供的类需要用@Configuration@ComponentScan进行批注。

当配置类与要加载的组件(bean)位于同一包中时,可以省略basePackage

 类似资料:
  • 我通过使用Spring和Hibernate创建实体、服务和服务的JUnit测试开始了我的项目。所有这些都很管用。然后,我添加了spring-mvc来使用许多不同的分步教程来制作这个web应用程序,但是当我试图使用注释制作Controller时,我在部署期间从Glassfish得到错误。我想由于某种原因Spring没有看到我的服务,但经过多次尝试,我仍然无法处理它。 /webapp/web-inf/

  • 我正在尝试建立一个简单的应用程序,以便在WildFly14上使用springboot 2.1.1和Java 11(因为Eclipse不支持WildFly15)。 部署失败,因为Wildfly在项目中找不到CDI BeanManager。我只是试图按照简单的(官方的)教程操作,所以我不明白为什么会出现错误。 这是代码: pom.xml application.java 这是StackTrace: 1

  • 我有一个安全配置类,其中有一个SecurityWebFilterChain bean。这个bean需要一个ServerHttpSecuitirty实例,但是spring说它找不到任何这种类型的beans,尽管在外部库(org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfiguration

  • 我正在开发一个连接到MySQL数据库的非常小的应用程序。 我正在尝试创建表记录,但得到“无事务处理”。 我已经准备好了所有合适的东西: 服务接口MyService及其实现MyServiceImpl 我已使用@service注释了服务impl 在控制器中,我为字段@Autowired MyService使用了接口名称 我有正确的事务配置,因为它最初是由Roo生成的 存在一个公共方法MyService

  • 我是一名学习使用jsp和Servlet构建Web应用程序的学生。一个月以来,我的Web应用程序项目一直运行良好,但今天它的行为突然变得奇怪了。当我提交jsp页面时,它无法找到我的servlet。我已经使用servlet注释来映射请求。 以下是我的JSP:- 以下是我的servlet:- 以下是我的控制台日志:-

  • 问题内容: 我正在运行Windows 8,但无法使javac正常工作。 我已将环境变量中的PATH设置为 我尝试过是否使用’;’ 但无济于事。 我最近在桌面上添加了这个问题;工作,但不是这种情况。 我确保javac确实也存在于bin中。 关于修复的任何建议将不胜感激。 编辑echo%PATH%给出: 确切的错误是:无法将“ javac”识别为内部或外部命令,可操作程序或批处理文件。 问题答案: 至