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

当包从hello更改为foo时,NoSuchBeanDefinitionException。

琴俊人
2023-03-14

当我按照https://spring.io/guides/gs/spring-boot/上提供的说明操作时,我可以使Spring Boot示例程序成功运行。

如果我将HelloService和HelloServiceImpl(如下所示的代码)添加到hello包中,并将HelloController.java更改为调用sayHello方法,那么它将按照预期工作,并且从http://localhost:8080中,我将看到如下所示的消息,正如预期的那样。

春靴的问候!helloService=来自HelloServiceImpl的Hello

现在,如果我将HelloService和HelloServiceImpl移动到foo包,那么在运行它时进行编译后,我得到如下所示的错误。

为什么spring Boot/Framework不能从“foo”包中获取所需的bean?但是,我能够验证它能够从hello包中成功地获取bean。

谢谢,

卡特彼勒HelloService.java

package hello;

public interface HelloService {

    public String sayHello();   

}

卡特彼勒HelloServiceImpl.java

package hello;
import org.springframework.stereotype.Service;


@Service("helloService")
public class HelloServiceImpl implements HelloService {

    /* (non-Javadoc)
     * @see com.apress.prospring3.springblog.service.HelloService#sayHello()
     */
    @Override
    public String sayHello() {
        return "Hello  from HelloServiceImpl ";
    }

}

在hellocontroller.java中,在index方法中更改return语句,如下所示,以便调用hello服务。

卡特彼勒HelloController.java

package hello;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

    @Autowired 
    private HelloService helloService;

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot! helloService = " + helloService.sayHello();
    }
}

$java-版本

java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

$which java

/usr/bin/java

lrwxr-xr-x 1 root wheel 74 3月9日22:00/usr/bin/java->/system/library/frameworks/javavm.framework/versions/current/commands/java

如果我将helloservice.java和helloserviceimpl.java从hello包移动到foo包,并且没有其他更改,那么当我尝试运行它时会得到以下错误。为什么它不能从'foo'包拾取,而它可以成功地从hello包拾取?如何解决此问题?这是否意味着这两个类必须驻留在hello包中才能工作?

Thx,

java.lang.reflect.InvocationTargetException
    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:483)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private foo.HelloService hello.HelloController.helloService; nested exception is org.springframework.beans.factory.**NoSuchBeanDefinitionException**: No qualifying bean of type [foo.HelloService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1180)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:660)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:552)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:293)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:749)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:738)
    at hello.Application.main(Application.java:17)
    ... 6 more

原因:org.SpringFramework.Beans.Factory.BeanCreationException:无法自动连接字段:private Foo.HelloService Hello.HelloController.HelloService;嵌套异常为org.springframework.beans.factory.noSuchBeanDefinitionException:未找到依赖项得[foo.helloservice]类型得合格bean:需要至少一个具有此依赖项自动候选资格得bean.依赖项批注:{@org.springframework.beans.factor.annotation.AutoWired(required=true)}(位于org.springframework.beans.factor.annotation.AutoWiredanNotationbeanpostprocessor.java:508)(位于org.springframework.beans.factor.annotation.injectionmetadata.inject(injectionmetadata.java:87)(位于依赖关系批注:{@org.springframework.beans.factory.annotation.AutoWired(required=true)}(位于org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.1060)(位于

共有1个答案

狄子真
2023-03-14

最初,当HelloService在hello包中时,Configuration类上的@ComponentScan告诉Spring在hello包中查找其他组件、配置和服务,从而允许它查找该类。现在由于您已经将HelloService移动到foo包中,@ComponentScan再也找不到它了;您还需要指定新的包名称,如:

    @ComponentScan({"hello","foo"})
 类似资料:
  • 问题内容: 具有基于输入模型的价格范围/评级功能。在加载时,从后端进行设置时,它以整数开始,但是当您键入它时,它变为字符串。Angular中有什么方法可以将输入的值声明为整数? HTML: JS: 问题答案: 我知道我来晚了,但我想我会发布这个答案,因为其他人可能仍在寻找替代方案。 您可以使用AngularJS指令链接功能解决此问题。代码: 然后,您将在输入元素上使用此指令,以确保将您输入的任何值

  • 这是我的密码 XML标记 风格 (聚焦视图) 现在我的问题 我希望底线更细一些。我应该在哪里换呢 有谁能告诉我解决问题的正确方法吗。任何人都需要更多的信息来回答这个问题。 提前感谢。

  • 我有以下班级档案 当我单独运行这个类时(作为- 控制台输出:[远程测试NG] 检测到测试NG版本 7.0.1 在端口 42250 上启动 Chrome 驱动程序 83.0.4103.39 (ccbf011cb2d2b19b506d833300373761342c20cd-refs/分支头/4103@{#416}) 仅允许本地连接。请参阅 https://chromedriver.chromium.

  • 当我滚动时,我正在使用。当您滚动到底部时,它将更新数据。更新数据后。返回顶部。理想情况下,我希望保留中的职位。我该怎么做呢? InnerClass

  • 当元素的值通过JavaScript代码更改时,会发生什么事件?例如: 事件在这里没有帮助,因为更改值的不是用户。 在Chrome上测试时,事件不会被激发。也许是因为元素没有失去焦点(它没有获得焦点,所以不能失去焦点)?