Spring Boot @autowired无法正常工作,类在不同的包中 [英] Spring Boot @autowired does not work, classes in different package

查看:691
本文介绍了Spring Boot @autowired无法正常工作,类在不同的包中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Boot应用程序.

I have a Spring boot application.

我收到以下错误

org.springframework.beans.factory.BeanCreationException:错误 创建名称为"birthdayController"的bean:自动接线的注入 依赖项失败;嵌套的异常是 org.springframework.beans.factory.BeanCreationException:无法 autowire字段:私人com.esri.birthdays.dao.BirthdayRepository com.esri.birthdays.controller.BirthdayController.repository;嵌套的 例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 类型为[com.esri.birthdays.dao.BirthdayRepository]的合格bean 找到依赖项:至少需要1个符合条件的bean 自动关联此依赖项的候选对象.依赖注释: {@ 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.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在或

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'birthdayController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.esri.birthdays.dao.BirthdayRepository com.esri.birthdays.controller.BirthdayController.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.esri.birthdays.dao.BirthdayRepository] 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:334) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at or

以下是我的存储库类的代码

Following is code of my Repository class

package com.esri.birthdays.dao;
import com.esri.birthdays.model.BirthDay;
public interface BirthdayRepository extends MongoRepository<BirthDay,String> {
    public BirthDay findByFirstName(String firstName);
}

以下是控制者.

package com.esri.birthdays.controller;
@RestController
public class BirthdayController {

    @Autowired
    private BirthdayRepository repository;

如果它们在同一包装中,则可以使用.不知道为什么

推荐答案

在示例包中使用@SpringBootApplication批注

When you use @SpringBootApplication annotation in for example package

com.company.config

com.company.config

它将自动进行组件扫描,如下所示:

it will automatically make component scan like this:

@ComponentScan("com.company.config") 

因此它将不会扫描com.company.controller等软件包.这就是为什么您必须在像这样的普通软件包之前的一个级别中在软件包中声明@SpringBootApplication的原因:com.company或使用scanBasePackages属性,如下所示:

So it will NOT scan packages like com.company.controller etc.. Thats why you have to declare your @SpringBootApplication in package one level prior to your normal packages like this: com.company OR use scanBasePackages property, like this:

@SpringBootApplication(scanBasePackages = { "com.company" })

OR componentScan:

OR componentScan:

@SpringBootApplication
@ComponentScan("com.company")


这篇关于Spring Boot @autowired无法正常工作,类在不同的包中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆