当前位置: 首页 > 知识库问答 >
问题:

获取错误考虑在配置中定义类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean

宁飞宇
2023-03-14

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

说明:com.sam.cmsShoppingCart.Controller.AdminPageController中构造函数的参数0需要一个类型为“com.sam.cmsShoppingCart.Models.PageRepository”的bean。操作:考虑在您的配置中定义一个类型为'com.sam.cmsshoppingcart.models.PageRepository'的bean。请找到下面的屏幕截图。

在此处输入图像描述

添加代码:

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

    public static void main(String[] args) {
        SpringApplication.run(CmsShoppingCartApplication.class, args);
    }
}
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";
    }
}
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";
    }
}
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;

}
<?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>

共有1个答案

谭鹏云
2023-03-14

显然,配置中缺少pageRepositorybean。在PageRepository的顶部添加@repository@component

@Repository
public interface PageRepository extends JpaRepository<Page, Integer>{}
 类似资料: