2016-03-21 6 views
0

Ich versuche, mit meiner Datenbankkonfigurationsdatei Verbindung zu zwei Datenbanken herzustellen. Wenn ich eine zweite Bean für die Datenquelle definiere, habe ich eine Ausnahme für eine nicht beschreibbare Eigenschaft. Welches ist der richtige Weg, um zwei Datenquellen zu definieren? Und muss ich etwas in meinen Java-Klassen tun?Spring Framework Verbindung zu mehreren Datenbanken xml JAVA

Daten source.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/test" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

</beans> 
+0

Hier ist eine Beschreibung, wie JPA-Annotationen mit mehreren Datenbanken zu verwenden, und wie sie konfigurieren mit '@ Configuration' http://www.baeldung.com/spring-data-jpa-multiple-databases –

+0

Diese Ein Beispiel ist, wie man es mit Hibernate macht (aber ich benutze es nicht). Ich finde schon die Lösung. – RockOrDead

Antwort

1

Ich finde die Lösung meines Problems.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/test" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="dataSourceSecond" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/test" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <bean id="jdbcTemplateSecond" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSourceSecond" /> 
    </bean> 

</beans> 
Verwandte Themen