春季错误“找不到类型为'java.lang.Class'的bean”

时间:2019-06-24 13:46:15

标签: 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”。错误,我找不到该错误的任何解决方案。希望你能帮助我。

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>

和我的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/**

1 个答案:

答案 0 :(得分:0)

您没有遵循本教程!如果您要学习教程,请仔细阅读。

GenericRepositoryImpl不能是Spring Repository。删除注释:

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

此外,您不能注入GenericRepository,而必须像创建TodoRepository的教程一样,创建从其扩展的接口