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有所了解和入門,都不會有什么問題。
之所以把這些簡單的內容拿出來寫,一是給自己在寫博客,或者說是記錄學習筆記的路上開一個簡單的頭,二是將自己認為會的東西進行梳理,進一步理解,鞏固自己的知識。
學習永遠都不是一件簡單的事情,可以有迷茫,可以懶惰,但是前進的腳步永遠都不能停止。
不積跬步,無以至千里;不積小流,無以成江海;