zoukankan      html  css  js  c++  java
  • @autowired与@qualifer的使用区别备忘


    使用@Autowired注释进行byType注入,如果需要byName(byName就是通过id去标识)注入,增加@Qualifier注释
    @Qualifer如果没有的话, 报的错如下:
    No unique bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: [transactionManager, jmsTransactionManager]

    原因:
    比如配置文件中有二个bean.
    <bean id="jmsTransactionManager"
            class="org.springframework.jms.connection.JmsTransactionManager">
            <property name="connectionFactory"
                ref="advancedConnectionFactory" />
        </bean>
    <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource">
                <ref bean="cpcDataSource" />
            </property>
        </bean>
    表面看起来是不同类型的类,但是由于在*Service里面注入的属性类型是PlatformTransactionManager.由于上面的二个bean都实现了这个接

    口.这样@autowired时,由于是bytype注入,就不能识别.此时就需要再加上@qualifer通过id去识别.


    而如果没有使用@Service的话,报错如下:
    No unique bean of type ..... expected at least 1 matching bean

    注意,这与上面的提示信息区别.
    
  • 相关阅读:
    java线程管理
    java进程间通信
    Possible multiple enumeration of IEnumerable
    Adding Search
    Working with SQL Server LocalDB
    ASP.NET Razor
    ASP.NET Razor
    modelstate.isvalid false
    Razor Intro
    open Command window here
  • 原文地址:https://www.cnblogs.com/highriver/p/2048667.html
Copyright © 2011-2022 走看看