在运行项目时。我在控制台中收到以下错误。

描述:
com.sam.cmsshoppingcart.controller.AdminPagesController中的构造函数的参数0需要找不到“ com.sam.cmsshoppingcart.models.PageRepository”类型的Bean。
行动:
考虑在配置中定义类型为“ com.sam.cmsshoppingcart.models.PageRepository”的bean。
请找到以下屏幕截图。

java - 遇到错误考虑在配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean-LMLPHP

java - 遇到错误考虑在配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean-LMLPHP

在此处输入图片说明

java - 遇到错误考虑在配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean-LMLPHP

java - 遇到错误考虑在配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean-LMLPHP

java - 遇到错误考虑在配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean-LMLPHP

java - 遇到错误考虑在配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean-LMLPHP

添加代码:

CmsShoppingCartApplication.Java

package com.sam.cmsshoppingcart;
@SpringBootApplication
public class CmsShoppingCartApplication {

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


WebConfig.Java

package com.sam.cmsshoppingcart;
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
     public void addViewControllers(ViewControllerRegistry registry) {
     registry.addViewController("/").setViewName("home");
    }
}


家用控制器

package com.sam.cmsshoppingcart.controller;

@Controller
public class HomeController {
    @GetMapping("/someRandomPage")
    public String home() {
        return "home";
    }
}


AdminPagesController.Java

package com.sam.cmsshoppingcart.controller;

@Controller
@RequestMapping("/admin/pages")
public class AdminPagesController {

    @Autowired
    private PageRepository pageRepo;

    public AdminPagesController(PageRepository pageRepo) {
        this.pageRepo = pageRepo;
    }

    @GetMapping
    public String index(Model model) {

        List<Page> pages =  pageRepo.findAll();

        model.addAttribute("pages", pages);

        return "/admin/pages/index";
    }
}


PageRepository.Java

package com.sam.cmsshoppingcart.models;
import org.springframework.data.jpa.repository.JpaRepository;
import com.sam.cmsshoppingcart.models.data.Page;

public interface PageRepository extends JpaRepository<Page, Integer>{

}


页面

package com.sam.cmsshoppingcart.models.data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Entity
@Table(name="pages")
@Data
public class Page {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String title;

    private String slug;

    private String content;

    private int sorting;

}


POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-entitymanager</artifactId>
           <version>5.3.7.Final</version>
        </dependency>
        <dependency>
           <groupId>org.springframework.data</groupId>
           <artifactId>spring-data-jpa</artifactId>
           <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <scope>runtime</scope>
           <version>5.1.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

最佳答案

显然,配置中缺少PageRepository bean。在@Repository顶部添加@ComponentPageRepository

@Repository
public interface PageRepository extends JpaRepository<Page, Integer>{}

09-25 22:03