繁体   English   中英

构建简单的Spring JPA项目时出现NoSuchBeanDefinitionException

[英]NoSuchBeanDefinitionException while building a simple Spring JPA project

我遵循以下教程: https : //spring.io/guides/gs/accessing-data-jpa/

我正在使用maven,Spring和JPA创建一个基于示例内存的数据库。 我有以下课程;

型号类别:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    public long getId() { return this.id; }
    public void setId(long Id) { this.id = Id; }

    private String firstName;
    public String getFirstName() { return this.firstName; }
    public void setFirstName(String FirstName) { this.firstName = FirstName; }

    private String lastName;
    public String getLastName() { return this.lastName; }
    public void setLastName(String LastName) { this.lastName = LastName; }

    public Customer(String firstname, String lastname) {
        super();
        this.firstName = firstname;
        this.lastName = lastname;
    }

    public Customer() {
        super();
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}

客户资源库类

import java.util.List;

import org.springframework.data.repository.CrudRepository;

import com.accenture.cursojava.modelos.Customer;

public interface CustomerRepository extends CrudRepository<Customer, Long>{
    List<Customer> findByLastName(String lastName);
}

应用类别:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

import com.accenture.cursojava.dataaccess.CustomerRepository;
import com.accenture.cursojava.modelos.Customer;

@Configuration
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        CustomerRepository repository = context.getBean(CustomerRepository.class);
        repository.save(new Customer("Cliente", "de Prueba 1"));
        repository.save(new Customer("Cliente", "de Prueba 2"));
        repository.save(new Customer("Cliente", "de Prueba 3"));
        repository.save(new Customer("Cliente", "de Prueba 4"));
        repository.save(new Customer("Cliente", "de Prueba 1"));
        repository.save(new Customer("Cliente", "de Prueba 2"));
        repository.save(new Customer("Cliente", "de Prueba 3"));
        repository.save(new Customer("Cliente", "de Prueba 4"));

        Iterable<Customer> clientes = repository.findAll();
        for (Customer customer : clientes) {
            System.out.println(customer.getFirstName());
            System.out.println(customer.getLastName());
        }
        Iterable<Customer> clientes1 = repository.findByLastName("de Prueba 1");
        for (Customer customer : clientes1) {
            System.out.println(customer.getFirstName());
            System.out.println(customer.getLastName());
        }
        context.close();
    }
}

执行完代码并按照教程指出的完全进行操作之后,我得到以下输出:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapplication.CustomerRepository] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:987)
    at com.myapplication.Application.main(Application.java:18)

有什么想法可能是罪魁祸首?

Spring对此bean的CustomerRepository一无所知。 您需要告诉Spring这是一个托管bean。 您可以通过添加@Repository批注来实现。

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>{
    List<Customer> findByLastName(String lastName);
}

您可能还需要告诉Spring在哪里查找带注释的类(尽管@EnableAutoConfiguiration可能使此操作变得不必要-我不熟悉它)

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories()//specify the base package containing your repository interfaces.
public class Application {

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM