小编Ani*_*til的帖子

Spring Boot @autowired不起作用,类在不同的包中

我有一个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)

如果它们在同一包装中则起作用. 不知道为什么

spring spring-mvc spring-boot

26
推荐指数
3
解决办法
8万
查看次数

标签 统计

spring ×1

spring-boot ×1

spring-mvc ×1