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有所瞭解和入門,都不會有什麼問題。

之所以把這些簡單的內容拿出來寫,一是給自己在寫博客,或者說是記錄學習筆記的路上開一個簡單的頭,二是將自己認爲會的東西進行梳理,進一步理解,鞏固自己的知識。

學習永遠都不是一件簡單的事情,可以有迷茫,可以懶惰,但是前進的腳步永遠都不能停止。

不積跬步,無以至千里;不積小流,無以成江海;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章