相当于@Primary的Spring XML [英] Spring XML equivalent of @Primary

查看:121
本文介绍了相当于@Primary的Spring XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在等效于 @ Primary 可以推广多个合格Bean之一

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

示例场景:

我有一个启用了自动配置功能的spring-boot应用程序.我定义了多个数据源,但是spring无法选择其中一个数据源作为其默认值.

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>

推荐答案

<bean>属性具有primary属性:

<bean primary="true|false"/>

记住:

如果通过XML声明了@Primary注释的类,则@Primary注释 元数据将被忽略,并遵守<bean primary="true|false"/> 代替.

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

这篇关于相当于@Primary的Spring XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆