我有两个要从我的Spring
应用程序访问的数据库。我SharedEntityManagerBean
为两个数据库配置了两个。这是配置:
<jpa:repositories base-package="xxx" entity-manager-factory-ref="entityManagerFactory1" />
<jpa:repositories base-package="xxx" entity-manager-factory-ref="entityManagerFactory2" />
<tx:annotation-driven/>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager1">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
<property name="dataSource" ref="dataSource1" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager2">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
<property name="dataSource" ref="dataSource2" />
</bean>
<bean id="entityManagerFactory1"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="xxxxxx"
....
</bean>
<bean id="entityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="xxxxx"
...
</bean>
<bean id="entityManager1" class="org.springframework.orm.jpa.support.SharedEntityManagerBean" >
<property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>
<bean id="entityManager2" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>
<bean id="dataSource1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
...
</bean>
<bean id="dataSourceOntology"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
...
</bean>
我有两个EntityLocators
不是由Spring
每个相应数据库中的访问实体管理的。它们看起来像这样:
public class SpringEntitiyLocator1 {
private EntityManager em;
public SpringEntitiyLocator1() {
}
private EntityManager getEM() {
if (em == null) {
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(RequestFactoryServlet.getThreadLocalServletContext());
SharedEntityManagerBean bean = context.getBean("entityManager1",SharedEntityManagerBean.class);
em = bean.getObject();
}
return em;
}
}
当我在 applicationContext 中只SharedEntityManagerBean
定义一个时,调用getBean()
工作正常。但是,一旦我SharedEntityManagerBean
定义了两个 s,我就会收到错误消息:
Bean named 'x' must be of type [y], but was actually of type [$Proxy]
我读到我应该通过添加到我的 application.xml 来使用cglib
代理。<aop:config proxy-target-class="true"/>
那是最好的解决方案吗?
当我添加该行时,我得到了Caused by: java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable
错误。
我需要aspectj
那个吗?
编辑:
如果我只有一个SharedEntityManagerBean
定义,我可以调用getBean(SharedEntityManagerBean.class)
. 这工作正常。
我调试了代码,似乎这个调用会调用getBean("&entityManager1",SharedEntityManagerBean.class)
(note & ) 。
但是,当我传递名称时,getBean("EntityManager1",SharedEntityManagerBean.class)
我得到一个类型转换异常。
同时SharedEntityManagerBean
定义 s 和 callgetBean
没有名称也会导致异常(找不到具有该名称的 bean)。
所以我目前的解决方法是调用:getBean("&entityManager1",SharedEntityManagerBean.class)
和getBean("&entityManager2",SharedEntityManagerBean.class)
这工作正常。