我有一个Spring启动应用程序.
我收到以下错误
org.springframework.beans.factory.BeanCreationException:创建名为'birthdayController'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.esri.birthdays.dao.BirthdayRepository com.esri.birthdays.controller.BirthdayController.repository; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到[com.esri.birthdays.dao.BirthdayRepository]类型的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)〜[spring-beans-4.2 .4.RELEASE.jar:4.2.4.RELEASE]在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.4.RELEASE.jar:4.2. 4.RELEASE] org.springframework上的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE].在org.springframework.beans.factory.support.AbstractBeanFactory $ 1中的beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]. getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at或
以下是我的Repository类的代码
package com.esri.birthdays.dao; import com.esri.birthdays.model.BirthDay; public interface BirthdayRepository extends MongoRepository{ public BirthDay findByFirstName(String firstName); }
以下是控制器.
package com.esri.birthdays.controller; @RestController public class BirthdayController { @Autowired private BirthdayRepository repository;
如果它们在同一包装中则起作用. 不知道为什么
在示例包中使用@SpringBootApplication批注时
com.company.config
它将自动进行组件扫描,如下所示:
@ComponentScan("com.company.config")
所以它不会像扫描等com.company.controller ..那包你为什么要在你的包你@SpringBootApplication像这样的普通包一个水平之前声明:com.company或使用scanBasePackages属性,就像这样:
@SpringBootApplication(scanBasePackages = { "com.company" })
OR componentScan:
@SpringBootApplication @ComponentScan("com.company")