【问题标题】:Spring Boot @autowired does not work, classes in different packageSpring Boot @autowired 不起作用,不同包中的类
【发布时间】:2016-03-25 20:28:39
【问题描述】:

我有一个 Spring Boot 应用程序。

我收到以下错误

org.springframework.beans.factory.BeanCreationException: 错误 创建名为“birthdayController”的 bean:注入 autowired 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配字段:私有 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] 在或

以下是我的 Repository 类的代码

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;

如果它们在同一个包中就可以工作。不知道为什么

【问题讨论】:

  • 你的主类在哪个包中?它的组件扫描是否涵盖存储库和控制器包?

标签: spring spring-mvc spring-boot


【解决方案1】:

当你在例如包中使用@SpringBootApplication注解时

com.company.config

它会像这样自动进行组件扫描:

@ComponentScan("com.company.config") 

所以它不会扫描 com.company.controller 等包。这就是为什么你必须在你的普通包之前在包中声明你的 @SpringBootApplication 一级:com.company 或使用 scanBasePackages 属性,如下所示:

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

或组件扫描:

@SpringBootApplication
@ComponentScan("com.company")


【讨论】:

  • 在我添加一些非实体类之后,我需要将“@EntityScan”更改为“@ComponentScan”
【解决方案2】:

只需将包放在@SpringBootApplication 标签内即可。

@SpringBootApplication(scanBasePackages = { "com.pkg1", "com.pkg2", .....})

告诉我。

【讨论】:

    【解决方案3】:

    尝试使用@ComponentScan("com.esri.birthdays") 注释来注释您的配置类。 一般来说:如果你的项目中有子包,那么你必须在项目根目录中扫描你的相关类。我猜你的情况是“com.esri.birthdays”。 如果您的项目中没有子包,则不需要 ComponentScan。

    【讨论】:

      【解决方案4】:

      试试这个:

          @Repository
          @Qualifier("birthdayRepository")
          public interface BirthdayRepository extends MongoRepository<BirthDay,String> {
              public BirthDay findByFirstName(String firstName);
          }
      

      并且在注入 bean 时:

          @Autowired
          @Qualifier("birthdayRepository")
          private BirthdayRepository repository;
      

      如果没有,请检查您的配置中的 CoponentScan

      【讨论】:

        【解决方案5】:

        只要它们包含在 @SpringBootApplication 类的同一个包(或子包)中,Spring Boot 就会自动处理这些存储库。要对注册过程进行更多控制,您可以使用 @EnableMongoRepositories 注释。 spring.io guides

        @SpringBootApplication
        @EnableMongoRepositories(basePackages = {"RepositoryPackage"})
        public class Application {
            public static void main(String[] args) {
                SpringApplication.run(Application.class, args);
            }
        }
        

        【讨论】:

          【解决方案6】:

          在我的情况下,@component 不起作用,因为我使用 new &lt;classname&gt;() 初始化了该类实例。

          如果我们在代码中的任何地方通过传统的Java方式初始化实例,那么spring不会将该组件添加到IOC容器中。

          【讨论】:

            【解决方案7】:

            对于此类问题,我最终将@Service 注释放在新创建的服务类上,然后选择了自动装配。 因此,请尝试检查那些没有自动装配的类,如果它们需要应用相应的必需注释(如@Controller@Service 等),然后再次尝试构建项目。

            【讨论】:

              【解决方案8】:

              默认情况下,在 Spring Boot 应用程序中,组件扫描是在主类所在的包内完成的。包外的任何 bean 都不会被创建,因此会出现上述异常。

              解决方案:您可以将 bean 移动到主 spring boot 类(这不是一个好方法),或者创建一个单独的配置文件并导入它:

              @Import({CustomConfigutation1.class, CustomConfiguration2.class})
              @SpringBootpplication
              public class BirthdayApplication {
              
              public static void main(String [] args) {
                  springApplication.run(BirthdayApplication.class, args );
              }
              

              }

              将 bean 添加到这些 CustomConfiguration 文件中。

              【讨论】:

                【解决方案9】:

                我遇到了同样的问题。当我从 Autowired 对象中删除私有修饰符时,它对我有用。

                【讨论】:

                  【解决方案10】:

                  肯定会有一个bean也包含与生日相关的字段 所以使用它,您的问题将得到解决

                  @SpringBootApplication
                  @EntityScan("com.java.model*")  // base package where bean is present
                  public class Application {
                      public static void main(String[] args) {
                          SpringApplication.run(Application.class, args);
                      }
                  }
                  

                  【讨论】:

                    【解决方案11】:
                    package com.test.springboot;
                            @SpringBootApplication
                        @ComponentScan(basePackages = "com.test.springboot")
                        public class SpringBoot1Application {
                    
                            public static void main(String[] args) {
                                ApplicationContext context=  SpringApplication.run(SpringBoot1Application.class, args);
                    

                    ================================================ =======================

                    package com.test.springboot;
                        @Controller
                        public class StudentController {
                            @Autowired
                            private StudentDao studentDao;
                    
                            @RequestMapping("/")
                            public String homePage() {
                                return "home";
                            }
                    

                    【讨论】:

                    • 在发布答案之前,最好先阅读帮助部分:stackoverflow.com/help/how-to-answer -- 您的答案有几个问题(格式,仅代码)
                    • 对不起...你能回答吗,我需要改变什么来运行上面的代码
                    【解决方案12】:

                    另一种有趣的解决方法是注释 setter 方法的参数。看来,对于 setter 方法(与构造函数不同),您必须将方法作为一个整体进行注释。

                    这对我不起作用: public void setRepository(@Autowired WidgetRepository repo)

                    但这确实: @Autowired public void setRepository(WidgetRepository repo)

                    (Spring Boot 2.3.2)

                    【讨论】:

                      【解决方案13】:

                      当我添加@ComponentScan("com.firstday.spring.boot.services")scanBasePackages{"com.firstday.spring.boot.services"} 时,jsp 未加载。因此,当我在 @SpringBootApplication 类中添加项目的父包时,它在我的情况下工作正常

                      代码示例:-

                      package com.firstday.spring.boot.firstday;
                      
                      import org.springframework.boot.SpringApplication;
                      import org.springframework.boot.autoconfigure.SpringBootApplication;
                      
                      @SpringBootApplication(scanBasePackages = {"com.firstday.spring.boot"})
                      public class FirstDayApplication {
                      
                          public static void main(String[] args) {
                              SpringApplication.run(FirstDayApplication.class, args);
                          }
                      
                      }
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 2022-07-22
                        • 1970-01-01
                        • 1970-01-01
                        • 2018-10-04
                        • 2019-01-23
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多