java - Spring 错误 "Required a bean of type ' java.lang.Class' 无法找到”

标签 java spring spring-boot spring-data-jpa

我开始使用 Spring,最近我想实现一个通用存储库。我遵循了本教程 ( https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-adding-custom-methods-into-all-repositories/ ),但我无法使其工作。

我不断收到“com.dani.demo.dao.GenericRepositoryImpl 中构造函数的参数 0 需要一个类型为‘java.lang.Class’的 bean,但无法找到。”错误,我找不到该错误的任何解决方案。希望你能帮助我。

通用存储库:

package com.dani.demo.dao;

import java.io.Serializable;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {

}

GenericRepositoryImpl:

package com.dani.demo.dao;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> {

    @PersistenceContext
    private final EntityManager em;

    public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);
        this.em = em;
    }

}

Cerveza服务:

package com.dani.demo.dao;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> {

    @PersistenceContext
    private final EntityManager em;

    public GenericRepositoryImpl(Class<T> domainClass, EntityManager em) {
        super(domainClass, em);
        this.em = em;
    }

}

CervezaServiceImpl:

package com.dani.demo.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dani.demo.dao.GenericRepository;
import com.dani.demo.model.Cerveza;

@Service
@Transactional
public class CervezaServiceImpl implements CervezaService {

    @Autowired
    private GenericRepository<Cerveza, Integer> d;

    @Override
    public List<Cerveza> list() {
        // TODO Auto-generated method stub
        return d.findAll();
    }

    @Override
    public Optional<Cerveza> get(int id) {
        // TODO Auto-generated method stub
        return d.findById(id);
    }

    @Override
    public void update(Cerveza c) {
        // TODO Auto-generated method stub
        d.save(c);
    }

    @Override
    public void add(Cerveza c) {
        // TODO Auto-generated method stub
        d.save(c);
    }

    @Override
    public void delete(int id) {
        // TODO Auto-generated method stub
        d.deleteById(id);
    }

}

和错误消息:

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

Description:

Parameter 0 of constructor in com.dani.demo.dao.GenericRepositoryImpl required a bean of type 'java.lang.Class' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'java.lang.Class' in your configuration.

此外,这是我的 pom.xml:

<?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 http://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.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dani</groupId>
    <artifactId>02-CervezasCRUDGeneric</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>02-CervezasCRUDGeneric</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

和我的application.properties:

spring.datasource.url= jdbc:mysql://localhost:3306/tablas_de_prueba?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username= rtgd
spring.datasource.password= trgdtt


# ===============================
# = Tomcat configurations
# ===============================
spring.datasource.tomcat.max-wait= 20000
spring.datasource.tomcat.max-active= 50
spring.datasource.tomcat.max-idle= 20
spring.datasource.tomcat.min-idle= 15

# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true

# ===============================
# = VIEW RESOLVER
# ===============================
spring.mvc.static-path-pattern=/resources/**

最佳答案

您没有遵循教程!如果您制作教程,请仔细阅读。

GenericRepositoryImpl 不得是 Spring 存储库。删除注释:

@Transactional
public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> {

此外,您无法注入(inject) GenericRepository,您必须创建一个从它扩展的接口(interface),就像教程中的 TodoRepository

关于java - Spring 错误 "Required a bean of type ' java.lang.Class' 无法找到”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56737977/

相关文章:

spring-boot - spring boot ssl 显示不安全

java - 自动化 Web 测试服务器部署

java - 为什么我在 Java 中收到 NoClassDefFoundError 错误?

java - 独立于系统日期和时间的java时钟

java - 识别包含 native 方法实现的库文件/源

Spring JDBC 无法加载 JDBC 驱动程序类 [oracle.jdbc.driver.OracleDriver]

java - 在 Spring Boot 中增加 HTTP Post maxPostSize

mysql - 在 Spring Security 中使用 mysql 数据库对用户进行身份验证?

spring-boot - Spring Boot 2 和 ResourceServerProperties

database - 用Spring Boot创建h2数据库,如果不存在则不要删除。桌面应用程序