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

时间:2014-04-12 02:30:06

标签: spring spring-boot

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

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

来自Spring Boot的问候! helloService =来自HelloServiceImpl的Hello

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

为什么spring boot / framework无法从 foo ' foo '中获取所需的bean。包?我能够验证它是否可以从 hello 包中成功获取bean。

谢谢,

cat HelloService.java

package hello;

public interface HelloService {

    public String sayHello();   

}

cat 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服务。

cat 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 -jar target / gs-spring-boot-0.1.0.jar

$ java -version

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)

$哪个java

的/ usr / bin中/ JAVA

$ ls -lt / usr / bin / java

lrwxr-xr-x 1根轮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 包中才能使其正常工作?

谢谢,

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符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)     在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)     ......还有22个 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[foo.HelloService]的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1060)     在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:920)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:815)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)     ......还有24个

1 个答案:

答案 0 :(得分:1)

最初当HelloService在hello包中时,Configuration类上的@ComponentScan告诉Spring在hello包中查找其他组件,配置和服务,允许它找到这个类。 既然你已经将HelloService移动到了foo包,那么@ComponentScan就找不到它了;您需要指定新的包名称,如:

    @ComponentScan({"hello","foo"})