Spring Framework 组件注册 之 @Component
Spring Framework 组件注册 之 @Component
写在前面
在spring大行其道的今天,对于spring的使用和掌握乃是不可缺少的必备技能。但是spring的整个体系尤为庞大,对它的学习,还得从基础一点一滴的慢慢积累。本文主要介绍
@Component
注解在spring中的简单使用,以及注解的派生性
和层次性
@Component 简单使用
@Component
注解是一个元注解,即可以标注在其它的注解上。在spring中,任何被@Component
注解标识的组件均为组件扫描的候选对象,并且被@Component
元注解标注的注解,在任何组件标注它时,也被视作组件扫描的候选对象。简单来说,就是在spring中,一个普通的javaBean被@Component
注解标记后,在使用基于注解配置和类路径扫描时,会被作为候选组件,添加到spring容器中
package com.spring.study.ioc.register; /** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */ @Data @Component public class TestComponent { private String id = "@Component"; }
添加spring启动引导类,以及spring启动时需要扫描的类路径
/** * spring 容器启动引导类 * * @author TangFD * @since 2019/6/25. */ @ComponentScan("com.spring.study.ioc.register") public class TestComponentBootstrap { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestComponentBootstrap.class); System.out.println("context id : " + applicationContext.getId()); TestComponent bean = applicationContext.getBean(TestComponent.class); System.out.println("TestComponent bean : " + bean); applicationContext.close(); } }
spring容器启动后,控制台打印的结果:
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@Component)
如此,在spring中通过简单在一个普通的javaBean上添加@Component
注解,再加上扫描类路径,就可以将该javaBean添加到spring容器中。spring就可以对这个Bean的生命周期进行管理。
前面提到
@Component
是一个元注解,当它标记在另一个注解上时,该组件同样会具有被spring扫描,并识别组件的能力。在spring中,被@Component
标记的注解有很多,例如:@Controller
,@Service
,@Repository
,当一个普通的javaBean被这些注解标注时,spring容器启动时同样会把该Bean视为候选组件,添加到容器中。
将上面TestComponent
类的注解换成@Service
,结果也是相同的
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service { @AliasFor(annotation = Component.class) String value() default ""; }
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */ @Data @Service public class TestComponent { private String id = "@Service"; }
spring容器启动后,控制台打印的结果:
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@Service)
派生性
和层次性
这两个概念源自慕课网中小马哥的课程介绍,严格上讲,注解是没有派生性和层次性的,之所以这样讲,是因为在spring中的很多注解都是有着派生性和层次性的结构。通过这两种特性,我们也可以自定义自己的注解,利用@Component
元注解,来将普通的javaBean扫描添加到spring容器中
派生性
自定义一个注解@FirstAnnotation
,被@Component元注解标识,并保持相同的签名,当有组件使用@FirstAnnotation 注解标注时,就会被spring容器扫描并加载
/** * 自定义注解@FirstAnnotation,被@Component标注 * @author TangFD * @since 2019/6/10. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface FirstAnnotation { String value() default ""; }
将上面TestComponent
类的注解换成@FirstAnnotation
,结果也是相同的
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */ @Data @FirstAnnotation public class TestComponent { private String id = "@FirstAnnotation"; }
spring容器启动后,控制台打印的结果:
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@FirstAnnotation)
层次性
Spring模式注解并不具有真正的派生性和层次性,只是像java类一样,具有类似继承和层次结构的功能
自定义一个注解@SecondAnnotation
,被@FirstAnnotation
注解标识,当有组件使用@SecondAnnotation
注解标注时,同样会被spring容器扫描并加载
/** * 自定义注解@SecondAnnotation ,被@FirstAnnotation标注 * * @author TangFD * @since 2019/6/11. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @FirstAnnotation public @interface SecondAnnotation { String value() default ""; }
将上面TestComponent
类的注解换成@SecondAnnotation
,结果也是相同的
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */ @Data @SecondAnnotation public class TestComponent { private String id = "@SecondAnnotation"; }
spring容器启动后,控制台打印的结果:
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@SecondAnnotation)
小结
本文介绍了spring中
@Component
元注解的简单使用,并通过示例说明了注解的继承和层次性功能。文章内容非常简单,只要对spring有所了解和入门,都不会有什么问题。
之所以把这些简单的内容拿出来写,一是给自己在写博客,或者说是记录学习笔记的路上开一个简单的头,二是将自己认为会的东西进行梳理,进一步理解,巩固自己的知识。
学习永远都不是一件简单的事情,可以有迷茫,可以懒惰,但是前进的脚步永远都不能停止。
不积跬步,无以至千里;不积小流,无以成江海;
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
LeetcCode 27:移除元素 Remove Element(python、java)
公众号:爱写bug 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 示例 1: 给定 nums = [3,2,2,3], va...
- 下一篇
Spring Framework 组件注册 之 @Import
Spring Framework 组件注册 之 @Import 写在前面 向spring中注册组件或者叫javaBean是使用spring的功能的前提条件。而且spring也提供了很多种方式,让我们可以将普通的javaBean注册到spring容器中,比如前一篇文章Spring Framework 组件注册 之 @Component中写的利用@Component注解将普通的javaBean注册到容器中,本文说的@Import注解也是spring Framework提供的将普通javaBean注册到容器中,以及后续文章会说的@Configuration,FactoryBean等方式。 @Import 注册普通Bean 使用@Import注册一个普通Bean,只需要在@Import注解中指定待注册Bean的class即可 /** * 使用Import注解,注册一个普通的Bean */ @Data public class TestImport { private String id = "@Import"; } 在spring启动引导类中,添加@Import注解 /** * spring ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Mario游戏-低调大师作品
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS8编译安装MySQL8.0.19
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS8安装Docker,最新的服务器搭配容器使用