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有所了解和入门,都不会有什么问题。

之所以把这些简单的内容拿出来写,一是给自己在写博客,或者说是记录学习笔记的路上开一个简单的头,二是将自己认为会的东西进行梳理,进一步理解,巩固自己的知识。

学习永远都不是一件简单的事情,可以有迷茫,可以懒惰,但是前进的脚步永远都不能停止。

不积跬步,无以至千里;不积小流,无以成江海;

Spring Framework 组件注册 之 @Component的更多相关文章

  1. Spring Framework 组件注册 之 @Import

    Spring Framework 组件注册 之 @Import 写在前面 向spring中注册组件或者叫javaBean是使用spring的功能的前提条件.而且spring也提供了很多种方式,让我们可 ...

  2. Spring Framework 组件注册 之 FactoryBean

    Spring Framework 组件注册 之 FactoryBean 前言 前两篇文章介绍了如何使用@Component,@Import注解来向spring容器中注册组件(javaBean),本文将 ...

  3. spring注解-组件注册

    一.@Configuration+@Bean @Configuration:配置类==配置文件 @Bean:给容器中注册一个Bean:类型为返回值的类型,默认是用方法名作为id @Bean(" ...

  4. Spring笔记 - 组件注册

    @Bean:类注入容器 xml方式: <bean id="person" class="com.hrh.bean.Person"> <prop ...

  5. 一、Spring之组件注册-@Configuration&@Bean给容器中注册组件

    xml配置方式 首先我们创建一个实体类Person public class Person { private String name; private Integer age; private St ...

  6. Spring Framework 条件装配 之 @Conditional

    Spring Framework 条件装配 之 @Conditional 前言 了解SpringBoot的小伙伴对Conditional注解一定不会陌生,在SpringBoot项目中,Conditio ...

  7. 一步步从Spring Framework装配掌握SpringBoot自动装配

    目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...

  8. Spring Boot之从Spring Framework装配掌握SpringBoot自动装配

    Spring Framework模式注解 模式注解是一种用于声明在应用中扮演“组件”角色的注解.如 Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的 ...

  9. 04 . Vue组件注册,数据交互,调试工具及组件插槽介绍及使用

    vue组件 组件(Component)是 Vue.js 最强大的功能之一. 组件可以扩展 HTML 元素,封装可重用的代码. 组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的 ...

随机推荐

  1. Method and apparatus for establishing IEEE 1588 clock synchronization across a network element comprising first and second cooperating smart interface converters wrapping the network element

    Apparatus for making legacy network elements transparent to IEEE 1588 Precision Time Protocol operat ...

  2. Android 项目编译过程

    Android 工程构建的持续集成,需要搭建一套编译和打包自动化流程,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个比较深入的理解,例如知道它的每一步 ...

  3. Windows10内置ubuntu子系统安装后中文环境设置

    原文:Windows10内置ubuntu子系统安装后中文环境设置 第一开启相关设置,使用小娜(Win键+c)直接查找关键字打开更快.   ①设置→查找"针对开发人员"→开发人员模式 ...

  4. 优秀开源项目之四:CrashRptProbe,查询程序奔溃的利器

    1.背景: 在开发人员进行项目开发和调试代码时,有一个非常困扰的问题,就是程序在调试运行过程中会莫名其妙地异常退出.由于导致异常退出的问题非常多,因此在面对这种无任何提示的异常退出时,开发人员会非常无 ...

  5. Archlinux 下Intel + NVIDIA 双显卡3D 游戏配置(dota2@steam)

    下午打了几场dota2 感觉流畅度还算非常不错的,写点东西记录一下.用Archlinux 的一般来说都会用搜索引擎,所以仅仅说下须要注意的地方就可以. 1. steam 自带的OpenGL 库是过时的 ...

  6. Monthly Expense

    Problem Description Farmer John is an astounding accounting wizard and has realized he might run out ...

  7. 本文摘录 - FlumeJava

    本文节选不保证论文的完整性和理解的准确性  原始的MapReduce.分Map,Shuffle,Reduce. Map里包含shards. Shuffle理解为groupByKey的事情.Reduce ...

  8. opengl实现直线扫描算法和区域填充算法

    总体介绍 1.   使用线性扫描算法画一条线,线性离散点 2.   利用区域填充算法画多边形区域,区域离散的点 开发环境VS2012+OpenGL 开发平台 Intel core i5,Intel H ...

  9. InstallUtil.exe版本引起安装windows services 服务遇到的问题,System.BadImageFormatException

    原文:把程序安装成windows服务的过程及遇到的问题 做好了定时任务的程序,要把它放在服务器上,作为windows服务运行,也就是说,退出登录,用户注销后程序任然在后台运行. 将exe程序发布为服务 ...

  10. WP8.1使用HttpClient类

    Uri uri = new Uri("http://www.cnsos.net/weburl/index.htm", UriKind.Absolute); HttpClient m ...