尝试在控制器Spring Boot上使用@Autowire储存库时获得NoSuchBeanDefinitionException吗?

时间:2019-03-04 07:14:50

标签: java spring maven spring-boot javabeans

我是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行中。 感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

docs说了@SpringBootApplication

  

@ComponentScan:在应用程序所在的软件包上启用@Component扫描

这意味着Spring将在您的com.bagyt.controller中查找bean,这与定义了@SpringBootApplication的类及其子包的包相同。

如果要扫描其他软件包中的组件,例如BagytApplication,则应移动com.bagyt软件包中的com.bagyt.repository

答案 1 :(得分:0)

使用@springBootApplication检查您的控制器程序包是类的相同程序包还是子程序包。如果任何依赖项(@ controller,@ Service,@ Repository)位于不同的程序包结构中,请使用@componentscan(给出程序包名称)。我认为它应该可以工作。

答案 2 :(得分:0)

在springboot主类上,您可以添加

@EnableJpaRepositories(basePackages = "com.bagyt.repositories") 
相关问题