无法引用PersonsRepository接口的实例(Unable to reference instance of PersonsRepository interface)

编程入门 行业动态 更新时间:2024-10-14 22:20:16
无法引用PersonsRepository接口的实例(Unable to reference instance of PersonsRepository interface)

我正在使用Spring数据教程: http : //spring.io/guides/tutorials/data/3/

我正在努力让我的Spring 4 + Hibernate应用程序启动并运行。 我根据指南创建了以下单元测试:

/** * */ package com.corrisoft.air.db.integration; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import com.corrisoft.air.db.JPAConfiguration; import com.corrisoft.air.db.repository.PersonsRepository; import com.corrisoft.air.model.Person; /** * @author Corrisoft Android Development * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {JPAConfiguration.class}) @Transactional @TransactionConfiguration(defaultRollback = true) public class PersonsRepositoryIntegerationTests { @Autowired PersonsRepository personsRepository; @Test public void thatItemIsInsertedIntoRepoWorks() throws Exception { int id = 42; Person person = new Person(); person.setId(id); person.setFirstName("Zaphod"); person.setLastName("Beeblebrox"); personsRepository.save(person); Person retrievedPerson = personsRepository.findById(id); assertNotNull(retrievedPerson); assertEquals(id, retrievedPerson.getId()); } } This tests the repository that I've created here: /** * */ package com.corrisoft.air.db.repository; import com.corrisoft.air.model.Person; import org.springframework.data.repository.CrudRepository; /** * @author Corrisoft Android Development * */ public interface PersonsRepository extends CrudRepository<Person, Integer>{ Person findById(long id); }

而且我得到以下异常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.corrisoft.air.db.repository.PersonsRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ... 28 more

文档说如果我有这样的JPAConfuration类,将从接口创建一个代理对象:

/** * */ package com.corrisoft.air.db; import java.sql.SQLException; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.orm.hibernate4.HibernateExceptionTranslator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.corrisoft.air.db.repository.PersonsRepository; /** * @author Corrisoft Android Development * */ @Configuration @EnableJpaRepositories(basePackages = "com.corrisoft.db.repository", includeFilters = @ComponentScan.Filter(value = { PersonsRepository.class }, type = FilterType.ASSIGNABLE_TYPE)) @EnableTransactionManagement public class JPAConfiguration { @Bean public DataSource dataSource() throws SQLException { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); return builder.setType(EmbeddedDatabaseType.H2).build(); } @Bean public EntityManagerFactory entityManagerFactory() throws SQLException { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.corrisoft.air.model"); factory.setDataSource(dataSource()); factory.afterPropertiesSet(); return factory.getObject(); } @Bean public EntityManager entityManager(EntityManagerFactory entityManagerFactory) { return entityManagerFactory.createEntityManager(); } @Bean public PlatformTransactionManager transactionManager() throws SQLException { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } @Bean public HibernateExceptionTranslator hibernateExceptionTranslator() { return new HibernateExceptionTranslator(); } }

我已经发现了教程的几个问题,我认为这是另一个问题。

I am working from the Spring data tutorial here: http://spring.io/guides/tutorials/data/3/

I am trying to get my Spring 4 + Hibernate app up and running. I have created the following unit test according to the guide:

/** * */ package com.corrisoft.air.db.integration; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import com.corrisoft.air.db.JPAConfiguration; import com.corrisoft.air.db.repository.PersonsRepository; import com.corrisoft.air.model.Person; /** * @author Corrisoft Android Development * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {JPAConfiguration.class}) @Transactional @TransactionConfiguration(defaultRollback = true) public class PersonsRepositoryIntegerationTests { @Autowired PersonsRepository personsRepository; @Test public void thatItemIsInsertedIntoRepoWorks() throws Exception { int id = 42; Person person = new Person(); person.setId(id); person.setFirstName("Zaphod"); person.setLastName("Beeblebrox"); personsRepository.save(person); Person retrievedPerson = personsRepository.findById(id); assertNotNull(retrievedPerson); assertEquals(id, retrievedPerson.getId()); } } This tests the repository that I've created here: /** * */ package com.corrisoft.air.db.repository; import com.corrisoft.air.model.Person; import org.springframework.data.repository.CrudRepository; /** * @author Corrisoft Android Development * */ public interface PersonsRepository extends CrudRepository<Person, Integer>{ Person findById(long id); }

And I'm getting the following exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.corrisoft.air.db.repository.PersonsRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ... 28 more

The docs say that a proxy object will be created from the interface if I have the JPAConfuration class like this:

/** * */ package com.corrisoft.air.db; import java.sql.SQLException; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.orm.hibernate4.HibernateExceptionTranslator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.corrisoft.air.db.repository.PersonsRepository; /** * @author Corrisoft Android Development * */ @Configuration @EnableJpaRepositories(basePackages = "com.corrisoft.db.repository", includeFilters = @ComponentScan.Filter(value = { PersonsRepository.class }, type = FilterType.ASSIGNABLE_TYPE)) @EnableTransactionManagement public class JPAConfiguration { @Bean public DataSource dataSource() throws SQLException { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); return builder.setType(EmbeddedDatabaseType.H2).build(); } @Bean public EntityManagerFactory entityManagerFactory() throws SQLException { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.corrisoft.air.model"); factory.setDataSource(dataSource()); factory.afterPropertiesSet(); return factory.getObject(); } @Bean public EntityManager entityManager(EntityManagerFactory entityManagerFactory) { return entityManagerFactory.createEntityManager(); } @Bean public PlatformTransactionManager transactionManager() throws SQLException { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } @Bean public HibernateExceptionTranslator hibernateExceptionTranslator() { return new HibernateExceptionTranslator(); } }

I've already found several issues with the tutorial and am thinking this is another.

最满意答案

您正在扫描错误的存储库包:

... basePackages = "com.corrisoft.db.repository" ...

而正确的是

import com.corrisoft.air.db.repository.PersonsRepository;

You are scanning wrong package for repositories:

... basePackages = "com.corrisoft.db.repository" ...

whereas the correct one is

import com.corrisoft.air.db.repository.PersonsRepository;

更多推荐

本文发布于:2023-08-07 02:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   接口   PersonsRepository   Unable   interface

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!