'Controller' 中构造函数的参数 0 需要找不到类型为 'service' 的 bean。不工作:添加注释、componentscans

Parameter 0 of constructor in 'Controller' required a bean of type 'service' that could not be found. Not working: adding annotations, componentscans

我正在尝试在我的项目中使用服务和 DTO(试图了解它们是什么以及它们应该如何使用)。首先,我只有控制器、模型和存储库。后端按预期工作。然后,我想我会使用 DTO 和服务将客户端与存储库连接起来。但是之后我的应用程序无法运行。

项目结构

MovieController:

package movie_API.movie.controllers;

import movie_API.movie.services.MovieService;
import movie_API.movie.services.beans.MovieDTO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Controller
//@RequestMapping("/something") <- if you want a general something before the other resources
public class MovieController {

    private final MovieService movieService;

    public MovieController(MovieService movieService) {
        this.movieService = movieService;
        System.out.println("build controller");
    }

    @GetMapping("/movies")
    public String movies(Model model) {
        System.out.println("before getting in");
        movieService.showMovies(model);
        System.out.println("after getting in");

        return "movie/movies-all";
    }
 }

电影模型+对应的构造函数和getters和setters:

package movie_API.movie.models;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Movie {

    private String id;
    private String metascore;
    private String title;
    private String year;
    private String description;
    private String genreId;

MovieRepository:

package movie_API.movie.repositories;

import movie_API.movie.models.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public interface MovieRepository extends JpaRepository<Movie, String> {

    default ArrayList<String> getGenreNames(List<Movie> movies, GenreRepository genreRepository) {
        ArrayList<String> genreNames = new ArrayList<>();
        String genreIdInMovie;
        String genreName;
        for (Movie m : movies) {
            genreIdInMovie = m.getGenreId();
            genreName = genreRepository.findById(genreIdInMovie).get().getName();
            genreNames.add(genreName);
        }
        return genreNames;
    }
}

MovieService:

package movie_API.movie.services;

import movie_API.movie.models.Movie;
import movie_API.movie.repositories.GenreRepository;
import movie_API.movie.repositories.MovieRepository;
import movie_API.movie.services.beans.MovieDTO;
import org.jvnet.hk2.annotations.Service;
import org.springframework.ui.Model;

import java.util.List;

@Service
public class MovieService {

    private final MovieRepository movieRepository;
    private final GenreRepository genreRepository;
    private List<Movie> movies;
    private Movie movie;

    public MovieService(MovieRepository movieRepository, GenreRepository genreRepository) {
        this.movieRepository = movieRepository;
        this.genreRepository = genreRepository;
    }

    public void showMovies(Model m) {

        m.addAttribute("allMovies", getMovies());
        m.addAttribute("allGenres", getGenreNames());

    }
}

MovieDTO + 对应的构造函数和getters和setters:

package movie_API.movie.services.beans;

import java.io.Serializable;

public class MovieDTO implements Serializable {

    private static final long serialVersionUID = -8040351309785589042L;

    private String id;
    private String metascore;
    private String title;
    private String year;
    private String description;
    private String genreId;

MovieApplication:

package movie_API.movie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MovieApplication {

    public static void main(String[] args) {
        SpringApplication.run(MovieApplication.class, args);
    }

}

问题是我收到一个 错误没有名为 MovieService[=64= 的 bean ].

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-03 14:41:28.664 ERROR 3504 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in movie_API.movie.controllers.MovieController required a bean of type 'movie_API.movie.services.MovieService' that could not be found.


Action:

Consider defining a bean of type 'movie_API.movie.services.MovieService' in your configuration.


Process finished with exit code 1

经过对此事的研究,我发现了几种推荐的解决方案:

  • 显然,它不是从包中读取 bean,因此我试图通过更改为 @SpringBootApplication(scanBasePackages={"movie_API.movie.services"}) 来专门要求它扫描组件服务。这在我不再收到“无法启动”错误的意义上起作用。但现在的问题是控制器不再被访问。所有调用均由“404 Not found”返回,并且未调用我放入 Controller 中的打印件。我试图将所有包放入列表中,但返回“启动失败”错误。
  • 另一种解决方案是仔细检查 MovieRepository 和 MovieService 是否具有注释 @Repository 和 @Service。这些最初也在那里,所以它没有帮助。
  • 我还尝试使 MovieController 中的 MovieService 位于构造函数之外并自动装配。还是一样的问题(没有bean)

您在 MovieService class 中导入了错误的服务注释。您应该导入的是:import org.springframework.stereotype.Service;

步骤 1) 检查 ServiceImpl class。

步骤 2) 将@Service 附加到 ServiceImpl class。

第 3 步问题已解决。