(九) spring 使用自定义限定符注解

本文通过一个具体的示例展示了如何在Spring框架中使用自定义限定符来精确指定要注入的Bean实例,解决了当存在多个实现相同接口的Bean时,如何明确指定使用哪一个Bean的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例一

  • 定义接口  CD.java
package interfacepackage;

public interface CD {
    void play();
}
  • 定义接口 player .java
package interfacepackage;

//定义一个播放器接口
public interface player {
    void play();
}
  • 定义接口的实现类   CD1.java
package bean;

import org.springframework.stereotype.Component;

import annotation_self.CD1_Annotation;
import annotation_self.CD_Annotation;
import interfacepackage.CD;
@Component
@CD_Annotation    //使用自定义限定符来标识这个bean,类似于bean的id
@CD1_Annotation //使用自定义限定符来标识这个bean,类似于bean的id
public class CD1  implements CD{

    @Override
    public void play() {
        System.out.println("我是 CD1");
        
    }
    
}
  • 定义接口的实现类   CD2.java
package bean;

import org.springframework.stereotype.Component;

import annotation_self.CD2_Annotation;
import annotation_self.CD_Annotation;
import interfacepackage.CD;
@Component
@CD_Annotation  //使用自定义限定符来标识这个bean,类似于bean的id属性
@CD2_Annotation //使用自定义限定符来标识这个bean,类似于bean的id属性
public class CD2  implements CD{

    @Override
    public void play() {
        System.out.println("我是 CD2");
        
    }
    
}
  • 定义接口的实现类 CDPlayer.java
package bean;

import interfacepackage.CD;
import interfacepackage.player;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import annotation_self.CD2_Annotation;
import annotation_self.CD_Annotation;

@Component("cdp")
public class CDPlayer implements player {

    @Autowired
    @CD_Annotation  
    @CD2_Annotation  //表示注入的bean的限定符必须有@CD_Annotation和@CD2_Annotation
    private CD cd;

    @Override
    public void play() {

        cd.play();
    }

}
  • 定义配置类 CDPlayerConfig.java
package config;

import interfacepackage.CD;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import bean.CDPlayer;

@Configuration
@ComponentScan(basePackages="bean")
public class CDPlayerConfig {

}
  • 自定义注解限定符  CD_Annotation .java
package annotation_self;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})  //该自定义注解限定符的作用目标
@Retention(RetentionPolicy.RUNTIME)
@Qualifier   //说明这是个自定义限定符
public @interface CD_Annotation {

}
  • 自定义注解限定符  CD1_Annotation .java
package annotation_self;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})  //该自定义注解限定符的作用目标
@Retention(RetentionPolicy.RUNTIME)
@Qualifier   //说明这是个自定义限定符
public @interface CD1_Annotation {

}
  • 自定义注解限定符  CD2_Annotation .java
package annotation_self;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE})  //该自定义注解限定符的作用目标
@Retention(RetentionPolicy.RUNTIME)
@Qualifier  //说明这是个自定义限定符
public @interface CD2_Annotation {

}
  • 编写测试类 Test.java
package test;

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

import config.CDPlayerConfig;
import bean.CDPlayer;

public class Test {
    public static void main(String[] args) {
        
        ApplicationContext context=new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        CDPlayer cdplayer=(CDPlayer)context.getBean("cdp");
        cdplayer.play();
        
        
    }
}

结果:

 

转载于:https://www.cnblogs.com/shyroke/p/6862574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值