Spring XML equivalent @Primary

Is there an XML equivalent to @Primary that can contribute to one of several qualifying beans

An example script:

I have a spring-boot application with autoconfiguration enabled. I have defined multiple data sources, however spring cannot choose one of the default data sources.

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: mysqlDataSource,oracleDataSource

      

datasources.xml

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

    <bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mcs" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>


    <bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:test" />
        <property name="username" value="scott" />
        <property name="password" value="tiger" />
    </bean>

    <bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

</beans>

      

+3


source to share


1 answer


<bean>

property has an attribute primary

:

<bean primary="true|false"/>

      



And remember:

If the @Primary

-annotated class is declared via XML, the annotation @Primary

metadata is ignored and <bean primary="true|false"/>

respected instead.

+7


source







All Articles