我目前正在开发一个在使用 JPA 和 QueryDSL 的 apache Karaf 下运行的 OSGi 应用程序。
我想知道是否可以将 Spring Data 与 QueryDSL 一起使用,而不是当前的方法。
这样做的原因是我发现 Spring 存储库非常有用,并且拥有一个用于 NoSQL 数据库访问的模板将来可能会很有用。
我尝试使用 OSGi 启动一个没有 Web 上下文的普通 Spring 应用程序,但是当它尝试加载 applicationContext.xml 或 ApplicationContext.class 时出现 ClassNoutFoundException。
我不想使用 Spring DM,因为它已停产。
基本上,想要尝试这种集成的唯一原因是为了 Spring Repositories,但如果您认为这没有必要,请告诉我。任何有关如何实现这一点或是否可以说服的信息都将受到欢迎。
谢谢
更新
我已经设法通过使用 org.eclipse.gemini.blueprint.context.support.OsgiBundleXmlApplicationContext
启动应用程序上下文来使 spring 工作。 applicationContext 在 OSGi 中作为服务导出,我可以通过调用它来获取我需要的所有 bean。
我现在遇到的问题是,当我声明 <jpa:repositories base-package="x.y.z" />
时,出现以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in URL [bundle://251.13:0/META-INF/spring/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109)[187:org.springframework.context:3.1.4.RELEASE]
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.registerBeanPostProcessors(AbstractDelegatedExecutionApplicationContext.java:502)[193:org.eclipse.gemini.blueprint.core:1.0.0.RELEASE]
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.registerBeanPostProcessors(AbstractDelegatedExecutionApplicationContext.java:451)[193:org.eclipse.gemini.blueprint.core:1.0.0.RELEASE]
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:306)[193:org.eclipse.gemini.blueprint.core:1.0.0.RELEASE]
at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)[193:org.eclipse.gemini.blueprint.core:1.0.0.RELEASE]
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:290)[193:org.eclipse.gemini.blueprint.core:1.0.0.RELEASE]
at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:137)[194:org.eclipse.gemini.blueprint.extender:1.0.0.RELEASE]
at java.lang.Thread.run(Thread.java:662)[:1.6.0_37]
Caused by: java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:142)[195:org.springframework.transaction:3.1.4.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)[195:org.springframework.transaction:3.1.4.RELEASE]
at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)[195:org.springframework.transaction:3.1.4.RELEASE]
at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:103)[195:org.springframework.transaction:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1475)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1443)[185:org.springframework.beans:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)[185:org.springframework.beans:3.1.4.RELEASE]
作为 JPA 提供者,我正在使用 OpenJPA。 entityManagerFactory 是我可以通过使用蓝图获得的服务。我想我需要在 <jpa:repositories base-package="x.y.z" />
中引用它,但是由于 applicationContext.xml 是由 spring 而不是蓝图读取的,我该怎么做?
如果有任何正确方向的提示,我将不胜感激。
谢谢
最佳答案
在您的代码中直接使用 Querydsl-SQL 和
- 它可以在 OSGi 中很好地工作,因为它不使用类加载、编织、增强、缓存和其他听起来不错但会导致困惑的技巧
- 您的代码将比任何“缓存增强”JPA 引擎运行得更快
- 其他人将能够理解您的代码(不像 JPA Criteria API 查询)
- 您将确切了解数据库服务器上运行的哪些 SQL 命令可以最大限度地缩短解决问题的时间
- 您的代码将与任何 ORM 工具一样独立于数据库
不要将 Spring、spring-data、JPA 和其他单体技术与 OSGi 作为一起使用
- 它们被设计为在一个整体系统中工作,其中一切都在一个应用程序上下文中,而不是在单独的包中
- 通过将这些技术与 OSGi 结合使用,您将花费大部分时间来修复此类错误并寻找解决方法
对此提出异议的人已经花费了大量时间来寻找此类解决方法。他们设法实现了一些业务逻辑。他们希望他们现在真正为每个概念问题找到了解决方法,并且他们下次不必花费相同的工作量。他们在 bidding fee auction .诚实的家伙!在某个地方,你知道我是对的 ;-)。
我是根据自己的经验说的
- 尝试了基于 Hibernate 和 Don't repeat the DAO 的完美堆栈IBM 的文章(早在 Spring-Data 炒作开始之前)。两次
- 写了 hibernate-osgi-adapter对于休眠 4.1.x
- Re-implemented OSGi Enterprise 规范的完整 JPA 章节
关于spring - 是否可以将 OSGi 与 Spring Data 集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018755/