问题描述
我是Spring Boot的新手。 我必须将我的应用程序连接到MySQL servver。 在创建应用程序的背面时,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)}
这是我的控制器课
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行中。 谢谢你的帮助!
1楼
正如所说的关于@SpringBootApplication
@ComponentScan:对应用程序所在的软件包启用@Component扫描
这意味着Spring将在com.bagyt.controller
查找bean,这与定义了@SpringBootApplication
注释的类及其子包的包相同。
如果要扫描com.bagyt.repository
等其他程序包中的组件,则应将BagytApplication
程序包中的com.bagyt
移动。
2楼
检查您的控制器软件包与@springBootApplication是类的相同软件包还是子软件包。 如果任何依赖项(@ controller,@ Service,@ Repository)在不同的包结构中,请使用@componentscan(给包名称)。我认为它应该可以工作。
3楼
在springboot主类上,您可以添加
@EnableJpaRepositories(basePackages = "com.bagyt.repositories")