(九) 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(); }
}
结果:

(九) spring 使用自定义限定符注解的更多相关文章
- spring:自定义限定符注解@interface, 首选bean
spring:自定义限定符注解@interface, 首选bean 1.首选bean 在声明bean的时候,通过将其中一个可选的bean设置为首选(primary)bean能够避免自动装配时的歧义性. ...
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- 这一次搞懂Spring自定义标签以及注解解析原理
前言 在上一篇文章中分析了Spring是如何解析默认标签的,并封装为BeanDefinition注册到缓存中,这一篇就来看看对于像context这种自定义标签是如何解析的.同时我们常用的注解如:@Se ...
- Spring bean自定义命名策略(注解实现)
我们都知道项目后台开发是从 控制层——业务层——mybatis层,@Controller.@Service.@Mapper...等等注解可以将对象自动加载到bean容器中,还能实现相应的功能,使用起来 ...
- Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!
前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类. 接下来介绍使用mybatis 常用注解以及如何传参数等数据库 ...
- Spring系列9:基于注解的Spring容器配置
写在前面 前面几篇中我们说过,Spring容器支持3种方式进行bean定义信息的配置,现在具体说明下: XML:bean的定义和依赖都在xml文件中配置,比较繁杂. Annotation-based ...
随机推荐
- jQuery中ajax如何返回值到上层函数
jQuery中ajax如何返回值到上层函数 一.总结 一句话总结: ajax的同步操作即可,设置 async: false, 二.jquery的同步操作 var can_submit=true; $. ...
- oracle中的trigger
https://blog.csdn.net/indexman/article/details/8023740/ https://www.cnblogs.com/sharpest/p/7764660.h ...
- python 混入类MixIn
写在前面 能把一件事情说的那么清楚明白,感谢廖雪峰的官方网站. 1.为什么要用混入类?(小白入门) 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. step1: 回忆一下 ...
- python脚本实现-excel二级统计
pandas和SQL数据分析实战视频教程 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2& ...
- openwrt环境中某个运行在host端的软件如何安装到openwrt的$(STAGING_DIR_HOST)/bin下
可参考示例:见'git grep host-build.mk package/{system,utils}'
- 一个Flask应用运行过程剖析
相信很多初学Flask的同学(包括我自己),在阅读官方文档或者Flask的学习资料时,对于它的认识是从以下的一段代码开始的: from flask import Flask app = Flask(_ ...
- MySql 里的IFNULL用法
IFNULL(expr1,expr2)的用法: 假如expr1 不为 NULL,则 IFNULL() 的返回值为 expr1; 否则其返回值为expr2.IFNULL()的返回值是数字或是字符串,具体 ...
- React调试——visual studio code
原文链接:Using React in Visual Studio Code 原文链接:Live edit and debug your React apps directly from VS Cod ...
- Go项目部署到服务器
-bash: ./main: cannot execute binary file 将 go build main.go 生成的文件上传到服务器后,./main 运行后出新的报错 env GOOS=l ...
- redcon, Redis兼容的服务器框架
源代码名称:redcon 源代码网址:http://www.github.com/tidwall/redcon redcon源代码文档 redcon源代码下载 Git URL: 复制代码 git:// ...