Ani*_*til 26 spring spring-mvc spring-boot
我有一个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<BirthDay,String> {
public BirthDay findByFirstName(String firstName);
}
Run Code Online (Sandbox Code Playgroud)
以下是控制器.
package com.esri.birthdays.controller;
@RestController
public class BirthdayController {
@Autowired
private BirthdayRepository repository;
Run Code Online (Sandbox Code Playgroud)
如果它们在同一包装中则起作用. 不知道为什么
jgr*_*jgr 61
在示例包中使用@SpringBootApplication批注时
com.company.config
它将自动进行组件扫描,如下所示:
@ComponentScan("com.company.config")
Run Code Online (Sandbox Code Playgroud)
所以它不会像扫描等com.company.controller ..那包你为什么要在你的包你@SpringBootApplication像这样的普通包一个水平之前声明:com.company或使用scanBasePackages属性,就像这样:
@SpringBootApplication(scanBasePackages = { "com.company" })
Run Code Online (Sandbox Code Playgroud)
OR componentScan:
@SpringBootApplication
@ComponentScan("com.company")
Run Code Online (Sandbox Code Playgroud)
只需将包放在 @SpringBootApplication 标签中。
@SpringBootApplication(scanBasePackages = { "com.pkg1", "com.pkg2", .....})
Run Code Online (Sandbox Code Playgroud)
让我知道。
尝试使用注释来注释您的配置类@ComponentScan("com.esri.birthdays")
。一般来说:如果您的项目中有子包,那么您必须在项目根目录中扫描相关类。我想对于你的情况来说它将是“com.esri.birthdays”。如果您的项目中没有子打包,则不需要 ComponentScan。
归档时间: |
|
查看次数: |
82916 次 |
最近记录: |