多个SharedEntityManagerBeans和ApplicationContext.getBean问题

时间:2013-08-01 14:26:43

标签: spring proxy applicationcontext

我想从我的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,我就会得到错误:

Bean named 'x' must be of type [y], but was actually of type [$Proxy]

我读到SO,我应该通过将cglib添加到我的application.xml来使用<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)(注意&amp; )。

然而,当我传递名称getBean("EntityManager1",SharedEntityManagerBean.class)时,我得到一个类型转换异常。

定义了SharedEntityManagerBean并且没有名称调用getBean也会导致异常(找不到具有该名称的bean)。

所以我目前的解决方法是致电:getBean("&entityManager1",SharedEntityManagerBean.class)getBean("&entityManager2",SharedEntityManagerBean.class)

这很好用。

1 个答案:

答案 0 :(得分:0)

好吧显然SharedEntityManagerBeanFactoryBean,为此,我必须在bean名称之前添加&amp; 来检索SharedEntityManagerBean

或者我可以打电话:

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

请参阅herehere以供参考。