0

我有两个要从我的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)

这工作正常。

4

1 回答 1

0

Ok 显然SharedEntityManagerBean是一个FactoryBeanand 为此我必须在 bean 名称之前添加&来检索SharedEntityManagerBean.

或者我可能只是打电话给:

em = context.getBean("entityManager",EntityManager.class);

请参阅此处此处以供参考。

于 2013-08-01T16:01:38.013 回答