问题描述
我正在尝试自动关联非Spring管理的类的依赖项.为此,我使用以下
I'm trying to autowire dependencies on a class that is not managed by Spring.To do that I use the following
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml")) {
AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
factory.autowireBean(this);
factory.initializeBean(this, "logJaxSoapMessageHandler");
context.refresh();
}
此实例依赖于服务,依赖于存储库(由Spring数据JPA管理).
This instance has a dependency on a service, which has a dependency on a repository (managed by Spring data JPA).
问题是:当我尝试执行findOne
操作时,出现此异常
Problem is : when I try to perform a findOne
operation, I get this Exception
Caused by: java.lang.IllegalStateException: EntityManagerFactory is closed
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.validateNotClosed(EntityManagerFactoryImpl.java:388)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.getCriteriaBuilder(EntityManagerFactoryImpl.java:357)
at sun.reflect.GeneratedMethodAccessor166.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:388)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:541)
at com.sun.proxy.$Proxy26.getCriteriaBuilder(Unknown Source)
at sun.reflect.GeneratedMethodAccessor166.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:203)
at com.sun.proxy.$Proxy29.getCriteriaBuilder(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:523)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne(SimpleJpaRepository.java:373)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:414)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:399)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
当我尝试使用entityManagerFactory
时,它似乎已经关闭(或者可能从未打开过?).
The entityManagerFactory
seems to be already closed (or maybe never opened?) when I try to use it.
当我手动为课程自动接线时,您是否知道如何打开此EntityManagerFactory
的线索?
Do you have a clue on how to get this EntityManagerFactory
open when I manually autowire the class?
推荐答案
M指出. Deinum ,我正在创建一个新的ApplicationContext
而不是获取现有的.
As pointed out by M. Deinum, I was creating a new ApplicationContext
instead of getting the existing one.
我更新了代码,使其看起来像这样
I updated my code to look like this
ApplicationContextProvider appContextProvider = new ApplicationContextProvider();
ApplicationContext applicationContext = appContextProvider.getApplicationContext();
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
factory.autowireBean(this);
factory.initializeBean(this, "logJaxSoapMessageHandler");
ApplicationContextProvider
的位置如下:
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;
public ApplicationContext getApplicationContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
context = ac;
}
}
这很好,因为我得到了先前初始化的EntityManagerFactory
.
This works fine, as I get the previously initialized EntityManagerFactory
.
这篇关于EntityManagerFactory已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!