码迷,mamicode.com
首页 > 编程语言 > 详细

SpringFramework|@Primary

时间:2018-11-01 15:10:28      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:www   movies   实例   多个   encoding   code   选择   ext   oca   

@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

SpringFramework|@Primary

标签:www   movies   实例   多个   encoding   code   选择   ext   oca   

原文地址:https://www.cnblogs.com/shwo/p/9889029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!