(九) 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 ...
随机推荐
- 为什么ROC曲线不受样本不均衡问题的影响
转自:https://blog.csdn.net/songyunli1111/article/details/82285266 在对分类模型的评价标准中,除了常用的错误率,精确率,召回率和F1度量外, ...
- perl 语法速查 | 模块安装
perl -MCPAN -e shell install Bio::SeqIO 或者直接perl -MCPAN -e 'install Excel::Writer::XLSX' 用cpan装不上,编译 ...
- OpenJudge计算概论-最大奇数与最小偶数之差的绝对值
/*============================================================= 最大奇数与最小偶数之差的绝对值 总时间限制: 1000ms 内存限制: ...
- 简易的CRM系统案例之SpringMVC+JSP+MySQL+myBatis框架版本
主要对上一版DAO框架的替换hibernate变成myBatis 简易的CRM系统案例之SpringMVC+JSP+MySQL+hibernate框架版本 src/mybatis.xml <?x ...
- AnimatorSet学习示例代码
package com.loaderman.customviewdemo; import android.animation.AnimatorSet; import android.animation ...
- Time类
public class Demo_Timer { /** * @param args * 计时器 * @throws InterruptedException */ public static vo ...
- 007-多线程-JUC集合-Queue-BlockingQueue接口以及ArrayBlockingQueue
0.BlockingQueue接口介绍 BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利 ...
- osg qt ifc
ui_ifcproject_20190702.h #pragma once /************************************************************* ...
- java调用科大讯飞流式(websocket)语音识别接口
要使用讯飞的能力,需先注册讯飞开发平台账号(讯飞官网参见https://www.xfyun.cn/). 再创建应用,点击右上角的控制台 -> 创建新应用: 每个应用都有一个appId,由这个ap ...
- Qt编写控件属性设计器9-数据库采集
一.前言 数据库作为数据源,在很多组态软件中使用非常多,指定数据库类型,填写好数据库连接信息,指定对应的数据库表和字段,采集间隔,程序按照采集间隔自动采集数据库数据,绑定到界面上的控件赋值显示即可.使 ...