【问题标题】:Error creating bean with name 'repositorySearchController' defined in URL在 URL 中定义名称为“repositorySearchController”的 bean 创建错误
【发布时间】:2021-04-10 08:01:56
【问题描述】:

目前我正在尝试运行我的 Spring Boot 应用程序。我跟着一个教程:https://www.youtube.com/watch?v=BtUdl9pZwR0

很遗憾,我收到了一个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:在 URL [jar:file:/C:/Users//.m2/repository/org/springframework/data/spring-data 中定义名称为“repositorySearchController”的 bean 创建错误-rest-webmvc/3.3.2.RELEASE/spring-data-rest-webmvc-3.3.2.RELEASE.jar!/org/springframework/data/rest/webmvc/RepositorySearchController.class]:通过构造函数参数0表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class] 中定义名称为“pagedResourcesAssembler”的 bean 创建时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.web.PagedResourcesAssembler]:工厂方法“pagedResourcesAssembler”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class] 中定义的名称为“pageableResolver”的 bean 时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver]:工厂方法“pageableResolver”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class] 中定义的名称为“sortResolver”的 bean 时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver]:工厂方法“sortResolver”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class] 中定义名称为“repositoryRestConfiguration”的 bean 创建错误:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法“repositoryRestConfiguration”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class] 中定义的名称为“repositories”的 bean 时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.repository.support.Repositories]:工厂方法“存储库”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为 'userRepository' 的 bean 时出错'映射上下文';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.Product.Product

ExcelController

public class ExcelExporter {
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;

    private List<Product> listProducts;

    public ExcelExporter(List<Product> listProducts) {
        this.listProducts = listProducts;
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet("Products");
    }

    private void writeHeaderRow(){
        Row row = sheet.createRow(0);

        CellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight(16);
        style.setFont(font);

        Cell cell = row.createCell(0);
        cell.setCellValue("Product ID");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue("Name");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("Kategorie");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("Text");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("Location");
        cell.setCellStyle(style);
    }
    private void writeDataRows(){
        int rowCount = 1;

        for (Product Products : listProducts) {
            Row row = sheet.createRow(rowCount);

            Cell cell = row.createCell(0);
            cell.setCellValue(Product.getId());

            cell = row.createCell(1);
            cell.setCellValue(Product.getName());

            cell = row.createCell(2);
            cell.setCellValue(Product.getCategory());

            cell = row.createCell(3);
            cell.setCellValue(Product.getText());

            cell = row.createCell(4);
            cell.setCellValue(Product.getLocation());

        }
    }
    public void export(HttpServletResponse response) throws IOException {
        writeHeaderRow();
        writeDataRows();

        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
}

Product:

@EnableJpaRepositories
@Entity
@Service
public class Product {
    private static Long id;
    private static String name;
    private static String category;
    private static String text;
    private static String location;

public Product() {}
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public static Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public static String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public static String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public static String getLocation() { return location; }

    public void setLocation(String location) {
        this.location = location;
    }
}

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-security</artifactId>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>net.sf.supercsv</groupId>
            <artifactId>super-csv</artifactId>
            <version>2.4.0</version>
        </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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.springframework.security</groupId>-->
<!--            <artifactId>spring-security-test</artifactId>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我做错了什么?

编辑: 这里有更多可能有用的代码:

ProductService

@Service
@Transactional
public class ProductService {

    @Autowired
    public ProductRepository repo;

    public List<Product> listAll() {
        return repo.findAll();
    }

    public void save(Product product) {
        repo.save(product);
    }

    public Product get(long id) {
        return repo.findById(id).get();
    }

    public void delete(long id) {
        repo.deleteById(id);
    }
}

ProductRepository

public interface ProductRepository extends JpaRepository<Product, Long> {

}

【问题讨论】:

  • 在您的主类中添加@ComponentScan({ "Full Qualified Package Name" }) 并尝试。 注意 : Full Qualified Package Name 应该是您的包名,例如:com.example.test

标签: java spring spring-boot spring-mvc


【解决方案1】:

ExcelExporter 应使用@RestController 标签进行注释。

如果您想使用带有@Service 标签注释的服务,您应该在控制器类中使用您想要的任何注入或带有@Autowired 注释的服务注入服务。

因为我的名声而无法添加评论,而你没有提及。

【讨论】:

  • 我实际上编辑了我的 Product 类,因为我忘记了重要的代码。在那种情况下,我忘了添加@Service 注解。现在很多错误都消失了。谢谢!
  • 很高兴能帮上忙!
【解决方案2】:

如果你想在方法上设置 JPA 注释,你应该用 @Access(AccessType.PROPERTY) 注释实体类

默认情况下,访问是在字段上,并且没有字段被定义为产品实体的标识符。因此错误:

 No identifier specified for entity: com.Product.Product

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 2021-10-03
    • 2018-04-25
    • 2023-04-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多