尝试在控制器 Spring 上启动 @Autowire 存储库时出现 NoSuchBeanDefinitionException?
Get NoSuchBeanDefinitionException when trying to @Autowire repository on controller Spring Boot?
我是 Spring Boot 中的新手。我必须将我的应用程序连接到 MySQL 服务器。在创建应用程序的后端时,Spring 中的 bean 出现问题。当我在我的服务器中尝试 运行 时,我收到错误消息:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bagyt.reposotories.UniversityRepository' available: expected at least 1 bean which qualifies as an autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这是我的控制器class
package com.bagyt.controller;
import com.bagyt.exception.ResourceNotFoundException;
import com.bagyt.model.University;
import com.bagyt.repositories.UniversityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/universityApi")
public class UniversityController {
@Autowired
UniversityRepository universityRepository;
@GetMapping("/university")
public List<University> getAllNotes() {
return universityRepository.findAll();
}
@PostMapping("/university")
public University createNote(@Valid @RequestBody University note) {
return universityRepository.save(note);
}
@GetMapping("/university/{id}")
public University getNoteById(@PathVariable(value = "id") Long universityId) {
return universityRepository.findById(universityId)
.orElseThrow(() -> new ResourceNotFoundException("University", "id", universityId));
}
@PutMapping("/university/{id}")
public University updateNote(@PathVariable(value = "id") Long universityId,
@Valid @RequestBody University universityDetails) {
University university = universityRepository.findById(universityId)
.orElseThrow(() -> new ResourceNotFoundException("University", "id", universityId));
university.setName(universityDetails.getName());
university.setEmail(universityDetails.getEmail());
university.setDescription(universityDetails.getDescription());
university.setPhotoLink(university.getPhotoLink());
University updatedNote = universityRepository.save(university);
return updatedNote;
}
@DeleteMapping("/university/{id}")
public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long universityId) {
University note = universityRepository.findById(universityId)
.orElseThrow(() -> new ResourceNotFoundException("University", "id", universityId));
universityRepository.delete(note);
return ResponseEntity.ok().build();
}
}`
我的资料库
package com.bagyt.repositories;
import com.bagyt.model.University;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UniversityRepository extends JpaRepository<University, Long> {
}
我已经测试并确保错误出在控制器中的@Autowired 行中。
感谢您的帮助!
正如 docs 所说 @SpringBootApplication
@ComponentScan: enable @Component scan on the package where the application is located
这意味着 Spring 将在您的 com.bagyt.controller
中查找 beans,即定义了带有 @SpringBootApplication
注释的 class 及其子包的同一个包。
如果您想扫描其他包中的组件,例如 com.bagyt.repository
.
,您应该将 BagytApplication
移动到 com.bagyt
包中
检查您的控制器包是 class 与 @springBootApplication 相同的包还是子包。如果任何依赖项(@controller,@Service,@Repository)在不同的包结构中使用@componentscan(给包名)。我认为它应该工作。
在springboot mainclass上,可以添加
@EnableJpaRepositories(basePackages = "com.bagyt.repositories")
我是 Spring Boot 中的新手。我必须将我的应用程序连接到 MySQL 服务器。在创建应用程序的后端时,Spring 中的 bean 出现问题。当我在我的服务器中尝试 运行 时,我收到错误消息:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bagyt.reposotories.UniversityRepository' available: expected at least 1 bean which qualifies as an autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这是我的控制器class
package com.bagyt.controller;
import com.bagyt.exception.ResourceNotFoundException;
import com.bagyt.model.University;
import com.bagyt.repositories.UniversityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/universityApi")
public class UniversityController {
@Autowired
UniversityRepository universityRepository;
@GetMapping("/university")
public List<University> getAllNotes() {
return universityRepository.findAll();
}
@PostMapping("/university")
public University createNote(@Valid @RequestBody University note) {
return universityRepository.save(note);
}
@GetMapping("/university/{id}")
public University getNoteById(@PathVariable(value = "id") Long universityId) {
return universityRepository.findById(universityId)
.orElseThrow(() -> new ResourceNotFoundException("University", "id", universityId));
}
@PutMapping("/university/{id}")
public University updateNote(@PathVariable(value = "id") Long universityId,
@Valid @RequestBody University universityDetails) {
University university = universityRepository.findById(universityId)
.orElseThrow(() -> new ResourceNotFoundException("University", "id", universityId));
university.setName(universityDetails.getName());
university.setEmail(universityDetails.getEmail());
university.setDescription(universityDetails.getDescription());
university.setPhotoLink(university.getPhotoLink());
University updatedNote = universityRepository.save(university);
return updatedNote;
}
@DeleteMapping("/university/{id}")
public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long universityId) {
University note = universityRepository.findById(universityId)
.orElseThrow(() -> new ResourceNotFoundException("University", "id", universityId));
universityRepository.delete(note);
return ResponseEntity.ok().build();
}
}`
我的资料库
package com.bagyt.repositories;
import com.bagyt.model.University;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UniversityRepository extends JpaRepository<University, Long> {
}
我已经测试并确保错误出在控制器中的@Autowired 行中。 感谢您的帮助!
正如 docs 所说 @SpringBootApplication
@ComponentScan: enable @Component scan on the package where the application is located
这意味着 Spring 将在您的 com.bagyt.controller
中查找 beans,即定义了带有 @SpringBootApplication
注释的 class 及其子包的同一个包。
如果您想扫描其他包中的组件,例如 com.bagyt.repository
.
BagytApplication
移动到 com.bagyt
包中
检查您的控制器包是 class 与 @springBootApplication 相同的包还是子包。如果任何依赖项(@controller,@Service,@Repository)在不同的包结构中使用@componentscan(给包名)。我认为它应该工作。
在springboot mainclass上,可以添加
@EnableJpaRepositories(basePackages = "com.bagyt.repositories")