注入自动装配失败,无法自动装配字段。

7

我知道这个问题已经被问了很多次,我冒着重复提问的风险(可能是两次、三次或四次),但是提出的解决方案似乎对我不起作用。

我遇到了令人头疼的无法自动装配错误。这是我第一次从头开始设置完整的Spring项目,所以我不太清楚问题出在哪里。

这是我的当前设置:

ProjectRepo:

package be.italent.repo;

import be.italent.model.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProjectRepo extends JpaRepository<Project, Integer> {

}

项目服务:

package be.italent.services;

import be.italent.model.Project;
import be.italent.repo.ProjectRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectService {

    @Autowired
    private ProjectRepo projectRepo;

    public List<Project> getAllProjects() {
        return projectRepo.findAll();
    }
}

项目RestController:

package be.italent.controllers;

import java.util.ArrayList;
import be.italent.services.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import be.italent.model.Project;

@RestController
@RequestMapping("/projects")
public class ProjectRestController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping(method = RequestMethod.GET, produces="application/json")
    public ArrayList<Project> getProjects(){
        ArrayList<Project> c = (ArrayList<Project>) projectService.getAllProjects();

        return c;
    }
}

spring-mvc.xml

...
<context:component-scan base-package="be.italent"></context:component-scan>
...

错误:

严重: 发送上下文初始化事件到类org.springframework.web.context.ContextLoaderListener的监听器实例时出现异常 org.springframework.beans.factory.BeanCreationException: 创建名为'projectRestController'的bean时注入自动装配依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException: 无法自动装配字段:private be.italent.services.ProjectService be.italent.controllers.ProjectRestController.projectService;嵌套异常是org.springframework.beans.factory.BeanCreationException: 创建名为'projectService'的bean时注入自动装配依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException: 无法自动装配字段:private be.italent.repo.ProjectRepo be.italent.services.ProjectService.projectRepo;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到类型为[be.italent.repo.ProjectRepo]的合格bean,用于此依赖项:预期至少有1个bean符合此依赖项的自动装配候选项。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.BeanCreationException: 无法自动装配字段:private be.italent.services.ProjectService be.italent.controllers.ProjectRestController.projectService;嵌套异常是org.springframework.beans.factory.BeanCreationException: 创建名为'projectService'的bean时注入自动装配依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException: 无法自动装配字段:private be.italent.repo.ProjectRepo be.italent.services.ProjectService.projectRepo;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到类型为[be.italent.repo.ProjectRepo]的合格bean,用于此依赖项:预期至少有1个bean符合此依赖项的自动装配候选项。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.Autowired

需要帮助吗?谢谢!

解决方案

将以下内容添加到我的spring-mvc.xml文件中:

<beans
...
jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

...

<jpa:repositories base-package="be.italent.repo" entity-manager-factory-ref="emf"/>
2个回答

7
当应用程序上下文初始化时出现问题时,Spring通常会给出这些冗长的堆栈跟踪。通常可以通过查看堆栈跟踪底部找出最终原因。
在您的情况下,您在底部看到了这个错误消息:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [be.italent.repo.ProjectRepo] found
这意味着Spring无法找到类型为ProjectRepo的bean。
您的ProjectRepo存储库接口位于be.italent.repo包中,该包是be.italent的子包,已进行组件扫描,因此问题不在于它位于错误的包中。
您是否忘记启用Spring Data JPA?
当您使用XML配置(就像您现在正在做的那样)时,必须在配置文件中有一个repositories XML标签:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/data/jpa
           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- Tell Spring Data JPA where your repository interfaces are -->
    <jpa:repositories base-package="be.italent.repo" />

    <!-- ... -->
</beans>

当您使用JavaConfig时,可以通过注释来完成:
@Configuration
@EnableJpaRepositories("be.italent.repo")
public class MySpringConfiguration {
    // ...
}

搞定了,我更新了原始帖子! - Dennie
问题是 - Spring Data 在运行时为 Repository 接口提供了具体的代理类 - 子类,这些子类是具体的类。在这里,您定义了 @Repository,但未启用 data jpa。因此,它无法提供具体的代理。上下文找不到您的 repo 接口的具体实现,然后它大声喊出它找不到实现 - 您无法实例化接口。 :) - Priyak Dey

-2

问题出在 ProjectRestController.java 文件中

您已经在 ProjectService 上使用了 @Autowired 注解

  1. 移除 @Autowired 注解。
  2. projectService 生成 getter 和 setter。
  3. 在 spring.xml 中设置一个 Bean 用于 ProjectService

现在运行您的应用程序。这对我有效。


这会改变什么吗?你只是用不同的配置编写相同的代码。他使用了@Autowired。你在xml中使用了相同的概念! - Priyak Dey
你试过这个吗?我之前也遇到了同样的错误,尝试了很多方法,看了很多文章,但都没有解决。所以我使用了spring.xml来注入这些东西。记住,你不需要改变所有的@Autowired注入,只需要找到类并在spring.xml中使用getter和setter即可。 - Sineth Lakshitha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接