弹簧错误"Required a bean of type 'java.lang.Class' that could not be found"



i'm开始与Spring一起使用,最近我想实现一个通用存储库。我遵循本教程(https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-jpa-jpa-tutorial-custom-custom-custom-methods-into-into-into-repositories/(很多。

我一直在com.dani.demo.dao.genericrepositoryimpl中获得"构造函数的参数0"。错误,我无法找到该错误的任何解决方案。希望你能帮我。

genericrepository:

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

cervezaservice:

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>

和我的应用程序。

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不得是弹簧存储库。删除注释:

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

此外,您还不能注入您必须创建一个从教程中使用TodorePository

在教程中延伸的界面的通用通用。

相关内容