Spring扩展———自定义bean组件注解
引言
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组件注解的更多相关文章
- Spring 实现自定义 bean 的扩展
Spring mvc 提供了扩展 xml 的机制,用来编写自定义的 xml bean ,例如 dubbo 框架,就利用这个机制实现了好多的 dubbo bean,比如 <dubbo:applic ...
- 将spring管理的bean使用注解的方式注入到servlet中
Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下: 1.创建一个AbstractServlet 抽象类,让你的所 ...
- 【Spring】装配Bean 组件扫描
实现自动装配需要用注解:注解分为 spring规范和java规范 ,java规范需要引入javax.inject 包 ,使用maven,直接引入. 从中可以看到 @Named @Inject属于jav ...
- (九) spring 使用自定义限定符注解
案例一 定义接口 CD.java package interfacepackage; public interface CD { void play(); } 定义接口 player .java p ...
- Spring容器对Bean组件的管理
Bean对象创建 默认是随着容器创建 可以使用 lazy-init=true:在调用 getBean 延迟创建 也可以用 <beans default-lazy-init="true& ...
- Spring扩展自定义的XML标签
在网上搜了许多,感觉不够全面,就找了官方文档,下面记录如何找到对应的文档进入 网上许多博客都是以dateformat为实例进行编写的,通过官方的foo,能够学到更多的东西,下面贴一段代码,在官方示例上 ...
- Spring Boot自定义starter必知必会条件
前言 在目前的Spring Boot框架中,不管是Spring Boot官方还是非官方,都提供了非常多的starter系列组件,助力开发者在企业应用中的开发,提升研发人员的工作效率,Spring Bo ...
- Spring扩展:替换IOC容器中的Bean组件 -- @Replace注解
1.背景: 工作中是否有这样的场景?一个软件系统会同时有多个不同版本部署,比如我现在做的IM系统,同时又作为公司的技术输出给其他银行,不同的银行有自己的业务实现(比如登陆验证.用户信息查询等) ...
- spring:自定义限定符注解@interface, 首选bean
spring:自定义限定符注解@interface, 首选bean 1.首选bean 在声明bean的时候,通过将其中一个可选的bean设置为首选(primary)bean能够避免自动装配时的歧义性. ...
- Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件
本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...
随机推荐
- Maxcompute-UNION数据类型对齐的方法
简介: 怎么对齐两段union脚本的数据类型 第1章 问题概述 1.1 UNION中隐式类型转换问题 近期参与的一个私有云项目要升级,因为maxcompute要升级到更新的版本,对之 ...
- “让专业的人做专业的事”,畅捷通与阿里云的云原生故事 | 云原生 Talk
简介: 如何借助阿里云强大的 IaaS 和 PaaS 能力去构建新一代的 SaaS 企业应用,从而给客户提供更好.更强的服务,这是畅捷通一直在思考和实践的方向.最终,畅捷通选定阿里云企业级分布式应用服 ...
- [Mobi] Android Studio arm 模拟器
从右下角 Configure 打开 AVD Manager. 点击 "Create New Device" 来创建新设备 选择TV 接着Next,然后用 Other Imag ...
- Niginx中Vue Router 历史(history)模式的配置
快速配置 将build后的文件直接丢到niginx目录下的html文件夹中,然后配置nginx.conf,就可以在快速的实现niginxhistory模式的配置了. location /{ # 可使用 ...
- python 打包成exe可执行文件
一.pyinstall打包 代码编写完成,如何在没有python环境的电脑上运行?编写了一个GUI程序,如何把文件打包好,发给别人直接使用?其实最简单的办法就是把.py源文件,打包成可执行程序员exe ...
- go http请求如果参数中带有"等特殊字符,参数传输可能会出现问题
编码完整的URL url.QueryEscape(urlStr) 编码完整的URL 如果我们要对完整的 URL 进行编码呢? 就是PHP中 urlencode() 函数的功能. 在 GO 语言下可以直 ...
- 微信小程序开发入门(一),Nodejs搭建本地服务器
1. 如何模拟真实环境中,读取服务端数据,首先需要使用Nodejs搭建一个本地服务器的环境. 在搭建web服务器之前,需要先安装node.js(安装版本最好为6.9.x) 安装后node.js,接下 ...
- python教程2:if...else...+循环
一.if判断 有单分支.双分支.多分支,下面就是一个多分支的案例: 二.缩进 三.for循环 四.while循环 五.其他 random模块 string模块
- uniapp中正确使用echart
uniapp中不能直接使用百度echart,要么就只能嵌入html,然后在html中进入echart进行使用,这样非常不方便, 下面介绍这个插件,对百度echart进行局部小改造,使他能在uniapp ...
- 高性能远程桌面Splashtop 居家办公首选软件
2020年,新冠疫情期间,各地提倡远程办公.居家办公.在家里怎么使用办公室的电脑?以Splashtop为代表的远程桌面控制软件也就被越来越多的人知晓和使用了. 什么是Splashtop远程桌面? Sp ...