zoukankan      html  css  js  c++  java
  • SpringFramework|@Primary

    @Primary的使用

    前述

    Java: 1.8

    Maven: 3

    SpringFramework版本以及各组件成员: 5.1.1.RELEASE

    • spring-context
    • spring-core
    • spring-beans

    @Primary表示当多个bean可以自动装配到单值依赖项时,应该优先选择特定的bean。如果候选者中只存在一个主bean,则它将成为自动装配的值。

    示例

    电影的Bean类: Movie. 将会有两个MovieBean, 而我们需要播放第一场电影 - 动作片. 接下来才是喜剧片.

    电影Bean - Movie.java

    package yag;
    
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class Movie {
    
        private String content;
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        public Movie(){
    
        }
    }
    

    播放者(BeanUser) - MovieShower.java

    package yag;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MovieShower {
    
        private Movie movie;
    
        // 注入Movie实例
        @Autowired
        public void setMovie(Movie movie) {
            this.movie = movie;
        }
    
        // 使用实例
        public void show(){
            // 播放影片内容
            System.out.println(movie.getContent());
        }
    }
    

    Java配置 - MovieConfig.java

    package yag;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    @Configuration
    public class MovieConfig {
    
        @Bean
        @Primary
        public Movie firstMovie(){
            // 用了这个方法来替代XML配置中的value属性以赋固定值.
            Movie movie = new Movie();
            movie.setContent("正在播放动作片");
            return movie;
        }
    
        @Bean
        public Movie secondMovie(){
            Movie movie = new Movie();
            movie.setContent("正在播放喜剧电影");
            return new Movie();
        }
    
        // BeanUser
        @Bean
        public MovieShower movieShower(){
            return new MovieShower();
        }
    }
    

    启用注解扫描 - spring-beans.xml

    <?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/>
        <context:component-scan base-package="yag"/>
    </beans>
    

    执行者

    package yag;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Runner {
    
        public static void main(String[] args){
            ApplicationContext c = new AnnotationConfigApplicationContext(MovieConfig.class);
            MovieShower shower = c.getBean(MovieShower.class);
            shower.show();
        }
    }
    

    运行结果

    正在播放动作片
    
    Process finished with exit code 0
    
  • 相关阅读:
    【原创】大叔经验分享(53)kudu报错unable to find SASL plugin: PLAIN
    【原创】大叔经验分享(52)ClouderaManager修改配置报错
    【原创】大数据基础之Impala(3)部分调优
    【原创】大数据基础之Kudu(3)primary key
    【原创】大叔经验分享(51)docker报错Exited (137)
    【原创】大数据基础之Logstash(5)监控
    【原创】大数据基础之Logstash(4)高可用
    【原创】Linux基础之vi&vim基础篇
    【原创】大叔经验分享(50)hue访问mysql(librdbms)
    【原创】大叔经验分享(49)hue访问hdfs报错/hue访问oozie editor页面卡住
  • 原文地址:https://www.cnblogs.com/shwo/p/9889029.html
Copyright © 2011-2022 走看看