【发布时间】:2026-01-13 19:30:02
【问题描述】:
我正在使用 Spring 和 Spring Data JPA。在 Tomcat 中部署我的应用程序时,出现以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'myController': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.service.MyService com.controller.MyController.myService; nested exception
is org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'MyService': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.repository.MyRepository com.service.MyService.myRepository; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
[com.repository.MyRepository] 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)}
以下是我的代码:
MyController.java
package com.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.domain.MyEntity;
import com.service.MyService;
@RestController
@RequestMapping(MyController.ROOT_RESOURCE_PATH)
public class MyController{
public static final String ROOT_RESOURCE_PATH = "/test";
@Autowired
private MyService myService;
@RequestMapping(value="/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyEntity> getAll() {
return myService.getAll();
}
}
MyService.java
package com.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.domain.MyEntity;
import com.repository.MyRepository;
@Service(value = "MyService")
public class MyService {
@Autowired
private MyRepository myRepository;
public List<MyEntity> getAll() {
return myRepository.findAll();
}
}
MyRepository.java
package com.repository;
import java.util.List;
import org.springframework.data.repository.Repository;
import com.domain.MyEntity;
public interface MyRepository extends Repository<MyEntity, Long> {
public List<MyEntity> findAll();
}
}
MyApplication-context.xml
<jpa:repositories base-package="com.repository" />
<context:component-scan base-package="com.service" />
<context:component-scan base-package="com.controller" />
<context:annotation-config />
【问题讨论】:
-
你有没有用
@Repository注释MyRepository的实现类? -
将@Repository 注解添加到您的存储库中并试一试。由于您使用的是旧式组件扫描存储库的方法,因此您需要将它们识别为 Spring
-
@Arpit:OP 使用 Spring Data,它生成实现。他无法注释存储库。
-
不应该是public List
findAll();当定义了用户类型的 Repository 时。为什么要列出 ?? -
这与 Spring Boot 有什么关系?另外添加
<context:annotation-config />不会添加任何东西,因为<context:component-scan />已经涵盖了。
标签: spring spring-data-jpa autowired spring-4