(九) 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 ...
随机推荐
- elasticsearch工作笔记002---Centos7.3安装最新版elasticsearch-7.0.0-beta1-x86_64.rpm单机版安装
新版本es安装问题: https://blog.csdn.net/lidew521/article/details/88091539
- 移动端安卓和 IOS 开发框架 Framework7 布局
对应的各种效果,Framework7 里面实现的方式比较多,这里我就只写我用的一种,样式有的自己修改了的,想看官方详细的参见 http://framework7.cn 一.手风琴布局Accordion ...
- 你的Node应用,对接分布式链路跟踪系统了吗?(一) 原创: 金炳 Node全栈进阶 4天前 戳蓝字「Node全栈进阶」关注我们哦
你的Node应用,对接分布式链路跟踪系统了吗?(一) 原创: 金炳 Node全栈进阶 4天前 戳蓝字「Node全栈进阶」关注我们哦
- SpringBoot+Mybatis+Maven+MySql小案例
数据准备: 建表t_user ,插入数据..... 创建工程 构建pom.xml <?xml version="1.0" encoding="UTF-8" ...
- osg机械臂施工模拟
线程 0x2278 已退出,返回值为 0 (0x0). =====IfcTreeWidget==slotObjectsSelected1IfcObjectAttributeExtraction === ...
- RVCT编译错误 Cannot obtain license for Compiler
找不到文件 找不到文件 Error: C9932E: Cannot obtain license for Compiler (feature compiler) with license versio ...
- C++线程互斥、同步
一.线程互斥 如果多个线程需要访问且可能修改同一个变量,那么需要加锁,保证同一时刻只有一个线程可以访问,这个动作即最小“原子操作” 方式1: 使用c++提供的类mutex,lock,unlock即可 ...
- SQLite 的简单使用
程序运行结果如下 : 输入name和age,点击"确认插入"按钮,即插入一条记录到数据库. 程序代码如下:(有详细注释,相信大家都看得懂哈!!!) //// ViewContro ...
- UIwindow ---密码框
程序运行显示结果如下 : 验证密码输入错误显示如下: 代码如下 : 1> //// PasswordInputWindow.m// UIWindow--密码框//// Created by ...
- 【python比较两个列表list】 python2 cmp() python3 operator模块
https://www.yiibai.com/python/list_cmp.html 如果元素的类型相同,则执行比较并返回结果.如果元素是不同的类型,则检查它们是否是数字. 如果数字,必要时进行数字 ...