Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring XML equivalent of @Primary

Tags:

java

spring

xml

Is there an XML equivalent of @Primary that can promote one of the multiple qualifying beans

Example Scenario:

I've a spring-boot application with auto-configuration turned on. I've defined multiple datasources, however spring is unable to pick one of the datasources as its default.

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>
like image 603
Monish Sen Avatar asked Aug 07 '17 13:08

Monish Sen


People also ask

What is @primary in Spring Boot?

@Target(value={TYPE,METHOD}) @Retention(value=RUNTIME) @Documented public @interface Primary. Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.

What does @primary do in Spring?

@Primary annotation One way Spring can choose between two beans of the same type is by giving one bean priority over the other. The @Primary annotation is used for making a component the default choice when multiple beans of the same type are found.

What is the difference between @primary and @qualifier?

It's worth noting that if both the @Qualifier and @Primary annotations are present, then the @Qualifier annotation will have precedence. Basically, @Primary defines a default, while @Qualifier is very specific.

What is Spring XML configuration file?

XML configuration file format. The correlator-integrated messaging for JMS configuration files use the Spring XML file format, which provides an open-source framework for flexibly wiring together the difference parts of an application, each of which is represented by a bean.


1 Answers

<bean> property has primary attribute:

<bean primary="true|false"/>

And remember:

If a @Primary-annotated class is declared via XML, @Primary annotation metadata is ignored, and <bean primary="true|false"/> is respected instead.

like image 61
Mykola Yashchenko Avatar answered Oct 16 '22 01:10

Mykola Yashchenko