【问题标题】:NoSuchBeanDefinitionException while building a simple Spring JPA project构建简单的 Spring JPA 项目时出现 NoSuchBeanDefinitionException
【发布时间】:2014-09-26 21:59:53
【问题描述】:

我正在按照以下教程进行操作: 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);
    }
}

CustomerRepository 类

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)

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

【问题讨论】:

  • 你不应该用@EnableJpaRepositories注释你的代码吗?
  • 你实现了接口吗?我不相信您可以创建接口实例...
  • @CharlesSmartt 这是spring data 功能。它在引擎盖下实现了接口,当你要求它时,它会给你一个代理。这是一个美丽:-)
  • 听起来很不错。不知道那个。

标签: java spring maven jpa


【解决方案1】:

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 {

}

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 2020-01-04
    • 2016-10-15
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2018-02-20
    相关资源
    最近更新 更多