我一直在使用Spring数据和hibernate,我想知道什么是最好的类组织,避免浪费代码。 这是我的实际结构:
public interface FleetRepository extends JpaRepository<Fleet, Integer> {
}
然后我使用这个界面
public interface FleetServices {
//Create one row into Fleet table, if id already exists this method executes an update
public Fleet create(Fleet fleet);
//Check if a fleet with given id already exists
public boolean exists(int id);
}
因此我实施了它
@Service
public class FleetServicesImpl implements FleetServices{
@Resource
private FleetRepository fleetRepository;
@Override
@Transactional
public Fleet create(Fleet fleet) {
return fleetRepository.save(fleet);
}
@Override
@Transactional
public boolean exists(int id){
return fleetRepository.exists(id);
}
}
鉴于我有几个使用相同方法的类(例如save,create ...),我想创建一个用于所有这些方法的类通用,比如这个
public interface GeneralRepository<T, Id extends Serializable> extends JpaRepository<T, Id> {
}
然后
public interface GeneralServices<T,Id> {
//Create one row into T table, if id already exists this method executes an update
public T create(T object);
//Check if a row with given id already exists
public boolean exists(Id id);
//Find a row into T table by Id
public T findById(Id id);
}
最后
public class GeneralServicesImpl<T, Id extends Serializable > implements GeneralServices<T, Id> {
@Resource
private GeneralRepository<T,Id> generalRepository;
@Override
@Transactional
public T create(T object) {
return generalRepository.save(object);
}
@Override
@Transactional
public boolean exists(Id id) {
return generalRepository.exists(id);
}
@Override
@Transactional
public T findById(Id id) {
return generalRepository.getOne(id);
}
}
然后我将这个类用于我的FleetServicesImpl
@Service
public class FleetServicesImpl extends GeneralServicesImpl<Fleet, Integer> implements FleetServices {
}
所以在databaseServicesImpl
中@Service
public class DatabaseServicesImpl实现DatabaseServices {
@Autowired
private FleetServices fleetServices;
@Autowired
private EcuServicesImpl ecuServices;
public DatabaseServicesImpl() {
}
@Override
public void archiveAcquisition() {
ecuServices.create(new Ecu("MM"));
Ecu ecuId=ecuServices.findById("MM");
Fleet fleet=new Fleet(ecuId,"334","2.9",170,"5+","Full","4x2","MT");
System.out.println(ecuId.getIdEcu());
fleetServices.create(fleet);
}
} 但是我得到了这个例外
16:41:43.465 ERROR o.s.web.context.ContextLoader - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为&#39; databaseServicesImpl&#39;的注册自动连接依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.mkyong.services.EcuServicesImpl com.mkyong.services.DatabaseServicesImpl.ecuServices;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; ecuServicesImpl&#39;时出错:资源依赖注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; generalRepository&#39;的init时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:不是托管类型:class java.lang.Object 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)~ [spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)〜[spring-context-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)〜[spring-context-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)〜[spring-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)〜[spring-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)[spring-web-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)[catalina.jar:8.0.26] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)[catalina.jar:8.0.26] 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)[catalina.jar:8.0.26] at org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1408)[catalina.jar:8.0.26] at org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1398)[catalina.jar:8.0.26] at java.util.concurrent.FutureTask.run(FutureTask.java:266)[na:1.8.0_60] 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[na:1.8.0_60] 在java.lang.Thread.run(Thread.java:745)[na:1.8.0_60] 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.mkyong.services.EcuServicesImpl com.mkyong.services.DatabaseServicesImpl.ecuServices;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; ecuServicesImpl&#39;时出错:资源依赖注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; generalRepository&#39;的init时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:不是托管类型:class java.lang.Object 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] ...省略了22个常用帧 由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; ecuServicesImpl&#39;时出错:资源依赖注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; generalRepository&#39;的init时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:不是托管类型:class java.lang.Object 在org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311)〜[spring-context-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1145)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1069)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)〜[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] ...省略了24个常用帧 引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; generalRepository&#39;的init时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:不是manageset 29,2015 4:41:43 PM org.apache.catalina.core.StandardContext listenerStart
答案 0 :(得分:0)
Quoting Oliver Gierke(Spring Data Project Lead):
您获得的最后一个异常实际上表明您的JPA设置存在问题。 “不是托管bean”意味着不是JPA提供者所知道的类型。如果您正在设置基于Spring的JPA应用程序,我建议您在已配置为包含JPA实体的程序包的LocalContainerEntityManagerFactory上配置“packagesToScan”属性。或者,您可以在persistence.xml中列出所有实体类,但这通常更麻烦。
确保您的Spring配置设置正确,似乎并非所有类都被Spring识别。
要添加,heres an answer by Oliver关于Spring JPA中的通用存储库,因为您似乎正在使用它。