问题描述
项目中使用了spring data jpa,自己扩展了一个全局接口AbstractJxetRepositorypublic interface AbstractJxetRepository<T> {public void batchInster(List<T> list, int size) throws Exception;public void batchUpdate(List<T> list, int size) throws Exception;}实现类AbstractDao@NoRepositoryBeanpublic abstract class AbstractDao<T> implements AbstractJxetRepository<T> {@PersistenceContextprotected EntityManager em;@Transactionalpublic void batchInster(List<T> list, int size) throws Exception{for (int i = 0; i < list.size(); i++) {em.persist(list.get(i));if (i % size == 0) {em.flush();em.clear();}}}@Transactionalpublic void batchUpdate(List<T> list, int size) {for (int i = 0; i < list.size(); i++) {em.merge(list.get(i));if (i % size == 0) {em.flush();em.clear();}}}}在一个模块接口中继承AbstractJxetRepositorypublic interface EduUnitLevelDao extends PagingAndSortingRepository<EduUnitLevel, Integer>,JpaSpecificationExecutor<EduUnitLevel> {public List<EduUnitLevel> getBySchoolId(Integer schoolId);@SuppressWarnings("unchecked")public EduUnitLevel save(EduUnitLevel eduUnitLevel);public List<EduUnitLevel> save(List<EduUnitLevel> eduUnitLevels);现在的问题是,在service中调用EduUnitLevelDao ,会将batchInster方法中的batch当做EduUnitLevel的属性,提示No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevel全部的报错信息org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.com.jxet.datalayer.service.EduUnitLevelServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.jxet.datalayer.service.EduUnitLevelService cn.com.jxet.datalayer.service.EduUnitLevelServiceTest.eduUnitLevelService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at org.junit.runners.ParentRunner.run(ParentRunner.java:236)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.jxet.datalayer.service.EduUnitLevelService cn.com.jxet.datalayer.service.EduUnitLevelServiceTest.eduUnitLevelService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)... 26 moreCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)... 28 moreCaused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:601)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)... 39 moreCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1448)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:249)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)... 41 moreCaused by: org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)at org.springframework.data.repository.query.parser.Part.<init>(Part.java:72)at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:279)at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153)at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43)at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)... 49 more 问题补充:模块接口贴错了,应该是public interface EduUnitLevelDao extends PagingAndSortingRepository<EduUnitLevel, Integer>,JpaSpecificationExecutor<EduUnitLevel>,AbstractJxetRepository<EduUnitLevel> {public List<EduUnitLevel> getBySchoolId(Integer schoolId);@SuppressWarnings("unchecked")public EduUnitLevel save(EduUnitLevel eduUnitLevel);public List<EduUnitLevel> save(List<EduUnitLevel> eduUnitLevels);}
解决方案
No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevel 1、spring data jpa 没识别你这个东西 public void batchInster(List<T> list, int size) throws Exception; public void batchUpdate(List<T> list, int size) throws Exception; 2、你这么写不对的public abstract class AbstractDao<T> implements AbstractJxetRepository<T> { 3、注册有两种方式:3.1、如果单个模块需要UserRepository2.java 接口UserRepository2Impl.java 实现https://github.com/zhangkaitao/es/tree/master/common/src/test/java/com/sishuok/es/common/repository3.2、如果所有模块都需要:注册方式 <jpa:repositories base-package="com.sishuok.es.**.repository" repository-impl-postfix="Impl" factory-class="com.sishuok.es.common.repository.support.SimpleBaseRepositoryFactoryBean" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"> </jpa:repositories>https://github.com/zhangkaitao/es/blob/master/web/src/main/resources/spring-config.xml