引言

Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。

Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。 当然它也支持自定义 Java 标注。

Spring支持使用注解的方式配置bean和注入属性依赖关系,可以极大的减少XML配置文件的数量,特别是在Spirng Boot 和 Spring Cloud中,基本上都是使用注解来使用内置和自定义的bean对象,对开发人员而言,减少了繁琐的配置。

Component派生性

Component源码

/**
* Indicates that an annotated class is a "component".
* Such classes are considered as candidates for auto-detection
* when using annotation-based configuration and classpath scanning.
*
* <p>Other class-level annotations may be considered as identifying
* a component as well, typically a special kind of component:
* e.g. the {@link Repository @Repository} annotation or AspectJ's
* {@link org.aspectj.lang.annotation.Aspect @Aspect} annotation.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
String value() default ""; }

通过阅读Component注解我们得知

Component注解是作用于Class类上的,同时它在运行期有效(有一些注解是在编译时有效,比如一些编译插件lombak)

@Indexed是Spirng5.0出来的,用于编译译处理,加快Spring启动速度

通过查看其它bean组件注解(Configuration、Controller、Service、Repository等)的源码,我们发现这些能够标识bean组件的注解都是被Component注解了的

所以我们可以认为,Configuration等注解实际上都是Component的派生注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(annotation = Component.class)
String value() default "";
boolean proxyBeanMethods() default true; }

这是因为Spring在处理这些注解的时候都对标注在注解上的元注解进行了递归的处理,所有也就有了Component的派生性

通过这一点我们也可以利用Component的派生性,自定义一个由我们自己命名的注解

自定义基于Component的派生注解

需求:实现一个能够被spring识别的标注bean组件的注解,被这个注解标注了的bean是单例

实现SingletonComponent

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
//标注bean为单例bean
@Scope("singleton")
@Component
public @interface SingletonComponent {
//让这里的value值对应到Component中的value值,可以定义bean名称
@AliasFor(annotation = Component.class, attribute = "value")
String value() default "";
}

使用SingletonComponent

@SingletonComponent("xxstudent")
public class Student {
public Student() {
this.name = "xxxx" + new Random().nextInt();
}
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
'}';
}
}

测试

    public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Student.class);
Object xx1student = applicationContext.getBean("xxstudent");
if (xx1student instanceof Student) {
Student student = (Student) xx1student;
System.out.println(student);
} Object xx2student = applicationContext.getBean("xxstudent");
if (xx2student instanceof Student) {
Student student = (Student) xx2student;
System.out.println(student);
}
}

输出

Student{name='xxxx289881135'}
Student{name='xxxx289881135'}

可以看到:通过名称已经能够获取到bean对象了,同时这个对象也是单例的对象。

Spring扩展———自定义bean组件注解的更多相关文章

  1. Spring 实现自定义 bean 的扩展

    Spring mvc 提供了扩展 xml 的机制,用来编写自定义的 xml bean ,例如 dubbo 框架,就利用这个机制实现了好多的 dubbo bean,比如 <dubbo:applic ...

  2. 将spring管理的bean使用注解的方式注入到servlet中

    Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下: 1.创建一个AbstractServlet 抽象类,让你的所 ...

  3. 【Spring】装配Bean 组件扫描

    实现自动装配需要用注解:注解分为 spring规范和java规范 ,java规范需要引入javax.inject 包 ,使用maven,直接引入. 从中可以看到 @Named @Inject属于jav ...

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

    案例一 定义接口  CD.java package interfacepackage; public interface CD { void play(); } 定义接口 player .java p ...

  5. Spring容器对Bean组件的管理

    Bean对象创建 默认是随着容器创建 可以使用 lazy-init=true:在调用 getBean 延迟创建 也可以用 <beans default-lazy-init="true& ...

  6. Spring扩展自定义的XML标签

    在网上搜了许多,感觉不够全面,就找了官方文档,下面记录如何找到对应的文档进入 网上许多博客都是以dateformat为实例进行编写的,通过官方的foo,能够学到更多的东西,下面贴一段代码,在官方示例上 ...

  7. Spring Boot自定义starter必知必会条件

    前言 在目前的Spring Boot框架中,不管是Spring Boot官方还是非官方,都提供了非常多的starter系列组件,助力开发者在企业应用中的开发,提升研发人员的工作效率,Spring Bo ...

  8. Spring扩展:替换IOC容器中的Bean组件 -- @Replace注解

    1.背景:     工作中是否有这样的场景?一个软件系统会同时有多个不同版本部署,比如我现在做的IM系统,同时又作为公司的技术输出给其他银行,不同的银行有自己的业务实现(比如登陆验证.用户信息查询等) ...

  9. spring:自定义限定符注解@interface, 首选bean

    spring:自定义限定符注解@interface, 首选bean 1.首选bean 在声明bean的时候,通过将其中一个可选的bean设置为首选(primary)bean能够避免自动装配时的歧义性. ...

  10. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

随机推荐

  1. 科学地花钱:基于端智能的在线红包分配方案 (CIKM2020)

    简介: 红包是电商平台重要的用户运营手段,本文将介绍1688基于端智能技术开发的two-stage红包分发方案.这一方案持续在线上生效,相较于原有算法有明显提升. 一.前言 本文是作者在1688进行新 ...

  2. [FAQ] jsoneditor 如何切换 mode 或者选择 modes

    1. 用于切换编辑器模式:text.tree.code JSONEditor.setMode(mode) 2. 让 mode 变成可以选择的: const options = { modes: ['t ...

  3. IphoneX(10) 重启/关机, 强制重启/关机

    正常关机是同时长按 音量+ 和 右侧电源键,屏幕出现滑动按钮进行关机. 注意截图是同时短按 音量+ 和 右侧电源键. 强制关机是按照顺序按三个键:音量+   音量-  长按右侧键 Other:苹果X怎 ...

  4. 2019-2-27-VisualStudio-插件-翻译注释

    title author date CreateTime categories VisualStudio 插件 翻译注释 lindexi 2019-02-27 15:50:33 +0800 2019- ...

  5. fastreport .net打印普通报表

    fastreport .net打印普通报表 前言: .net代码层先不记录在这,后续会单独写一篇博客来记录. 直接在工具上进行功能点的实现 一.效果图 二.功能点 分页 分页小计 金额大写 三.功能点 ...

  6. postman使用中问题汇总

    当用postman来通过接口造数据时,读取参数化文件中身份证字段的值读取错误. 参数文件如下 选择参数文件后预览的数据如下 身份证号码全部变成了0000结尾的 解决方案: 需要将身份证号码用引号引起来 ...

  7. minicube安装

    minicube安装 一.安装手册: https://minikube.sigs.k8s.io/docs/start/ 二.安装 打开官网,选择和自己对应的系统和要下载的版本.点击下面的release ...

  8. Centos 密码过期问题 password has expired

    1.这个主要是由/etc/login.defs参数文件中的一些参数控制的的.它主要用于用户账号限制PASS_MAX_DAYS 60     #密码最大有效期,此处参数PASS_MAX_DAYS为60, ...

  9. 八大远程控制软件,完美替代Teamviewer

    理想情况下,最好的远程桌面软件应该物有所值,同时为用户提供快速.安全和可靠的远程连接.还应该有一套强大的解决方案,提供高级报告和增强功能.跨平台支持和通信. TeamViewer 拥有超过 25 亿的 ...

  10. 热烈祝贺 Splashtop 赢得最佳远程桌面用户满意度得分

    在 G2 的 2021 年冬季远程桌面网格报告中,Splashtop 的净发起人得分(NPS)为 93,是所有远程桌面工具中最高的. 在一份分析用户对 30 多种远程桌面解决方案的评论的报告中,Spl ...