我正在按照以下教程进行操作: https://spring.io/guides/gs/accessing-data-jpa/
我正在使用 maven、Spring 和 JPA 创建一个基于内存的示例数据库。我有以下类(class);
模型类:
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)
你知道罪魁祸首是什么吗?
最佳答案
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 {
}
关于java - 构建简单的 Spring JPA 项目时出现 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068901/