使用SpringData设置2个没有bean注释的DataBases时出错

时间:2017-09-29 14:09:12

标签: spring hibernate spring-data spring-data-jpa

我在xml applicationContext.xml中尝试配置2个dataSource时遇到问题。 我发现使用bean注释进行配置的示例。 我需要在我的实际架构中使用xml进行配置。

我看到了教程:

http://www.baeldung.com/spring-data-jpa-multiple-databases

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources

但我没有解决我的问题,这个Spring页面中使用的方法使用注释。我不能使用注释,我的配置是在xml中。 当我尝试应用seconf数据源时出错。

在添加第二个数据源之前,工作没问题! 添加第二个数据源时不起作用。

  

我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                        http://www.springframework.org/schema/jee 
                        http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
                        http://www.springframework.org/schema/util 
                        http://www.springframework.org/schema/util/spring-util-4.2.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task-4.2.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/data/jpa 
                        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
    default-autowire="byName" default-lazy-init="true">

    <context:annotation-config />

    <context:component-scan base-package="br.com.myProject" />

    <jpa:repositories base-package="br.com.myProject.ged.repository"/>

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
        <property name="jndiName" value="java:jboss/datasources/sgedDS" />
        <property name="resourceRef" value="true" />    
    </bean>

    <bean id="dataSourceNurer" class="org.springframework.jndi.JndiObjectFactoryBean"  scope="singleton">
        <property name="jndiName" value="java:jboss/datasources/nurerDS" />
        <property name="resourceRef" value="true" />    
    </bean>


    <!--  ************** ENTITY MANAGER SGED ******************** -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="br.com.myProject.ged.entity" />
        <property name="dataSource" ref="dataSource" />  
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

    <!--  ************** ENTITY NURER NURER ******************** -->    

    <bean id="entityManagerFactoryNurer" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="br.com.myProject.ged.entity" />
        <property name="dataSource" ref="dataSourceNurer" /> 

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>

    <!--  ******** SGED  ******** -->

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!--  ******** NURER  ******** -->

    <bean id="transactionManagerNurer" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryNurer" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" /> <!-- SGED -->
    <tx:annotation-driven transaction-manager="transactionManagerNurer" /> <!-- NURER -->


    <bean id="persistenceExceptionTranslationPostProcessor" 
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="br.com.myProject.ged.spring.SpringViewScope" />
                </entry>
            </map>
        </property>
    </bean>

</beans>
  

我的Bean服务层:

@Transactional (transactionManager = "transactionManager2")
    public List<DataBase2Entity> getAll(){
        return nurerSituacaoIdrRepository.findAll();
        // return new ArrayList<DataBase2Entity>();
    }


@Transactional (transactionManager = "transactionManager")
    public List<DataBaseEntity> getAll(){
        return nurerSituacaoIdrRepository.findAll();
        // return new ArrayList<DataBaseEntity>();
    }
  

我的BaseDao.java

public abstract class BaseDao<T>  {

    private Class<T> entityClass;

    @PersistenceContext(unitName = "entityManagerFactory")
    private EntityManager em;


    @PersistenceContext(unitName = "entityManagerFactoryNurer")
    private EntityManager emNurer;

    @SuppressWarnings("unchecked")
    public BaseDao() {
        this.entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

...

更新时间:02/10/2017 错误执行时间:

Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values

我使用相同的entityManagerFactory或创建另一个entityManagerFactory(参见BaseDao.java)。

1 个答案:

答案 0 :(得分:0)

这似乎是XML的一个问题。 我看到最后一行#105上有一个. (点)

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>.

删除圆点然后重试。