zoukankan      html  css  js  c++  java
  • Spring框架参考手册(4.2.6版本)翻译——第三部分 核心技术 6.9.3 使用@Primary微调基于注解的自动装配

    由于按类型自动装配可能会多个候选,因此通常需要对选择过程进行更多控制。实现这一目标的一种方法是使用Spring@Primary @Primary表明当多个bean可以自动装配到单值依赖项时,应该优先选择定的bean。如果候选者中只存在一个primary”bean,则它将是自动装配的值。

    假设我们有以下配置将firstMovieCatalog定义为primary MovieCatalog

    @Configuration
    public class MovieConfiguration { @Bean @Primary public MovieCatalog firstMovieCatalog() { ... } @Bean public MovieCatalog secondMovieCatalog() { ... } // ... }

    通过这样的配置,以下MovieRecommender将与firstMovieCatalog进行自动装配。

    public class MovieRecommender {
    
        @Autowired
        private MovieCatalog movieCatalog;
    
        // ...
    
    }

    相应的bean定义如下所示。

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        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">
    
        <context:annotation-config/>
    
        <bean class="example.SimpleMovieCatalog" primary="true">
            <!-- inject any dependencies required by this bean -->
        </bean>
    
        <bean class="example.SimpleMovieCatalog">
            <!-- inject any dependencies required by this bean -->
        </bean>
    
        <bean id="movieRecommender" class="example.MovieRecommender"/>
    </beans>

  • 相关阅读:
    sql server中使用链接服务器访问oracle数据库
    biztalk中使用信封(Envelope)消息
    EMS SQL Manager 2007 for MySQL发布
    MySQL Connector/NET
    Silverlight相关资源
    ADO.NET嵌套数据绑定
    收到网上订得书了,开始充电...
    几个.net下的ajax框架
    Visual Studio 2008 Beta 2 初步体验
    .Net Remoting常用部署结构
  • 原文地址:https://www.cnblogs.com/springmorning/p/10391189.html
Copyright © 2011-2022 走看看