(九) spring 使用自定义限定符注解
案例一
- 定义接口 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(); } }
结果: