SpringFramework|@Qualifier与泛型配合使用

@Qualifier与泛型配合使用

前述

Java: 1.8

Maven: 3

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

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

除了@Qualifier注释之外,您还可以使用Java泛型类型作为隐式的限定形式。 (@Senyag)

示例(基于注解配置)

背景:

电影院影片播放程序, MovieBean是影片的模板, 它规定了电影的内容(具有数据类型的content). MovieShower是电影院的放映程序, 它将获取不同的MovieBean进行放映. Runner是工作人员, 它负责调用放映程序MovieShower中的方法.

目的: 先播放动作片, 然后是《黑客帝国》.

影片模板Bean - MovieBean.java

不同的影片将会是不同的数据类型.

package yag;

import org.springframework.context.annotation.Configuration;

@Configuration
public class MovieBean<S> {

    private S content;

    public S getContent() {
        return content;
    }

    public void setContent(S content) {
        this.content = content;
    }
}

影片放映程序(BeanUser) - MovieShower.java

有两个方法, 一个放映动作片, 一个放映特定的黑客帝国.

放映程序将获取专用类型的Bean(动作片内容类型是String, 而黑客帝国的是Integer), 所以需要特定类型的Bean.

package yag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MovieShower {

    @Autowired
    private MovieBean<String> wantActionMovie;

    @Autowired
    private MovieBean<Integer> wantTheMatrix;

    public void showActionMovie(){
        // 将动作片内容(字符串)放进MovieBean
        wantActionMovie.setContent("动作片正在放映");
        // 播放(打印)
        System.out.println(wantActionMovie.getContent());
    }

    public void showTheMatrix(){
        // 将黑客帝国的内容(Integer)放进MovieBean
        wantTheMatrix.setContent(101011); 
        // 播放(打印)
        System.out.println(wantTheMatrix.getContent());
    }
}

配置文件 - CinemaConfig.java

package yag;

import org.springframework.context.annotation.Bean;

public class CinemaConfig {

    // 放映程序
    @Bean
    public MovieShower movieShower(){
        return new MovieShower();
    }

    // 动作片
    @Bean
    public MovieBean<String> actionMovieBean(){
        return new MovieBean<String>();
    }


    @Bean // 黑客帝国
    public MovieBean<Integer> theMatrixMovieBean(){
        return new MovieBean<Integer>();
    }
}

扫描配置文件 - 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>

执行者 - Runner.java

package yag;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Runner {

    public static void main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(CinemaConfig.class);
        MovieShower movieShower = context.getBean(MovieShower.class);

        System.out.println("下面放映动作片");
        movieShower.showActionMovie();
        System.out.println(".......");
        System.out.println("下面放映《黑客帝国》"); 
        movieShower.showTheMatrix();
    }
}

执行结果

下面放映动作片
动作片正在放映
.......
下面放映《黑客帝国》
101011

Process finished with exit code 0

(@Senyag)

posted @ 2018-10-28 15:26  Senyag  阅读(408)  评论(0编辑  收藏  举报