我知道这个问题已经问了很多,我冒着双重(或三重或四重)主题的风险,但是建议的解决方案似乎对我不起作用。
我遇到了很多无法自动连线的错误。第一次从头开始设置一个完整的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> {
}
ProjectService:
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();
}
}
ProjectRestController:
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:无法自动装配字段:私有be.italent.repo.ProjectRepo be.italent.services.ProjectService。projectRepo; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[be.italent.repo.ProjectRepo]的合格Bean作为依赖项:至少应有1个符合此依赖项自动候选条件的bean。依赖注释:org.springframework.bean上的org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)上的{@ org.springframework.beans.factory.annotation.Autowired(required = true)}。 org.springframework.beans.factory.support上的factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)org.springframework.beans.factory.support上的AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)266)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)在java.lang.Thread.run(Thread.java) :745)由以下原因引起: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上的org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)上的{@ org.springframework.beans.factory.annotation.Autowired(required = true)}。 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)上的beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)... 22更多原因:org.springframeworkbean。 .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.bean上的{@ org.springframework.beans.factory.annotation.Autowired(required = true)}位于org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)。 org.springframework.beans的factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)。在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement(org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)处的support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957) AutowiredAnnotationBeanPostProcessor.java:480)...还有24个原因: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。org.springframework.beans.factors.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)上的springframework.beans.factory.annotation.Autowired(required = true)}。 .inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)... 35更多原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有资格找到类型为[be.italent.repo.ProjectRepo]的Bean以获得依赖关系:期望至少有1个符合此依赖关系的自动装配候选者。依赖项注释:org.springframework上的{@ org.springframework.beans.factory.annotation.Autowired(required = true)}。
有什么帮助吗?谢谢!
解
将此添加到我的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"/>
当初始化应用程序上下文时出现问题时,Spring经常给出这些长堆栈跟踪。通常,您可以通过查看堆栈跟踪的底部来找出最终的原因。
在您的情况下,您会在底部看到此错误消息:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[be.italent.repo.ProjectRepo]的合格bean
这意味着Spring找不到类型为的bean ProjectRepo
。
您的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 {
// ...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句