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>

  • 相关阅读:
    黑盒测试和白盒测试的区别
    alpha测试和beta测试的区别
    selenium退出语句区别
    QTP8.2--安装流程
    Xshell无法连接Linux虚拟机问题
    Linux-----centos6.2---安装Linux的流程
    MySql错误处理--错误代码和消息
    基于Linux系统--web环境搭建
    前端底层-作用域 this 原型笔试题练习
    前端底层-冒泡与捕获
  • 原文地址:https://www.cnblogs.com/springmorning/p/10391189.html
Copyright © 2011-2022 走看看