我不能有两个数据源配置JPA Spring XML WildFly

时间:2017-02-18 19:49:42

标签: java spring hibernate jpa wildfly

xml中的Spring配置

<jee:jndi-lookup id="datas1" expected-type="javax.sql.DataSource"
    jndi-name="java:/jndi1" />

<jee:jndi-lookup id="datas2" expected-type="javax.sql.DataSource" 
    jndi-name="java:/jndi2" />

<bean id="EntityM"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="datas1" />
    <property name="packagesToScan" value="package.persistence.entity" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle12cDialect" />
        </bean>
    </property>
</bean>

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

The Excpetion是:

上下文初始化失败:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionJdbcOperations' defined in class path resource [org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.class]: Unsatisfied dependency expressed through method 'springSessionJdbcOperations' parameter 0: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: datas1,datas2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: datas1,jndi2.

我不能配置两个数据源,第一个数据源是entityManagar,第二个数据源是调用存储过程。

1 个答案:

答案 0 :(得分:0)

JdbcSessionConfiguration需要一个名为'dataSource'的'DataSource'类型的bean。 如果找不到,spring尝试使用'DataSource'类型的bean,忽略名称。因为有这种类型的两个bean,所以会抛出异常。

解决方案: 重命名要与EntityManager一起使用的DataSource bean 'datas1'到'dataSource'。

Spring应该能够创建JdbcSessionConfiguration,你可以根据需要使用'datas2'。

相关问题