分類
發燒車訊

【Spring註解驅動開發】使用@Import註解給容器中快速導入一個組件

寫在前面

我們可以將一些bean組件交由Spring管理,並且Spring支持單實例bean和多實例bean。我們自己寫的類,可以通過包掃描+標註註解(@Controller、@Servcie、@Repository、@Component)的形式將其註冊到IOC容器中,如果不是我們自己寫的類,比如,我們在項目中引入了一些第三方的類庫,此時,我們需要將這些第三方類庫中的類註冊到Spring容器中,該怎麼辦呢?此時,我們就可以使用@Bean和@Import註解將這些類快速的導入Spring容器中。接下來,我們來一起探討下如何使用@Import註解給容器中快速導入一個組件。

項目工程源碼已經提交到GitHub:https://github.com/sunshinelyz/spring-annotation

註冊bean的方式

向Spring容器中註冊bean通常有以下幾種方式:

  • 包掃描+標註註解(@Controller、@Servcie、@Repository、@Component),通常用於自己寫的類。
  • @Bean註解,通常用於導入第三方包中的組件。
  • @Import註解,快速向Spring容器中導入組件。

@Import註解概述

Spring 3.0之前,創建Bean可以通過xml配置文件與掃描特定包下面的類來將類注入到Spring IOC容器內。而在Spring 3.0之後提供了JavaConfig的方式,也就是將IOC容器里Bean的元信息以java代碼的方式進行描述。我們可以通過@Configuration與@Bean這兩個註解配合使用來將原來配置在xml文件里的bean通過java代碼的方式進行描述

@Import註解提供了@Bean註解的功能,同時還有xml配置文件里 標籤組織多個分散的xml文件的功能,當然在這裡是組織多個分散的@Configuration

先看一下@Import註解的源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
    /**
      * {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
      * or regular component classes to import.
      */
     Class<?>[] value();
}

從源碼里可以看出@Import可以配合 Configuration , ImportSelector, ImportBeanDefinitionRegistrar 來使用,下面的or表示也可以把Import當成普通的Bean使用。

@Import只允許放到類上面,不能放到方法上。下面我們來看具體的使用方式。

@Import註解的使用方式

@Import註解的三種用法主要包括:

  • 直接填class數組方式
  • ImportSelector方式【重點】
  • ImportBeanDefinitionRegistrar方式

注意:我們先來看第一種方法:直接填class數組的方式,其他的兩種方式我們後面繼續講。

@Import導入組件的簡單示例

沒有使用@Import註解的效果

首先,我們創建一個Department類,這個類是一個空類,沒有成員變量和方法,如下所示。

package io.mykit.spring.plugins.register.bean;

/**
 * @author binghe
 * @version 1.0.0
 * @description 測試@Import註解的bean
 */
public class Department {
}

接下來,我們先在SpringBeanTest類中創建testAnnotationConfig7()方法,輸出Spring容器中所有的bean,來查看是否存在Department類對應的bean實例,以此來判斷Spring容器中是否註冊有Department類對應的bean實例。

@Test
public void testAnnotationConfig7(){
    ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig2.class);
    String[] names = context.getBeanDefinitionNames();
    Arrays.stream(names).forEach(System.out::println);
}

運行SpringBeanTest類的testAnnotationConfig7()方法,輸出的結果信息如下所示。

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
personConfig2
person
binghe001

可以看到Spring容器中並沒有Department類對應的bean實例。

使用@Import註解的效果

我們在PersonConfig2類上添加@Import註解,並將Department類標註到註解中,如下所示。

@Configuration
@Import(Department.class)
public class PersonConfig2 {

此時,我們再次運行SpringBeanTest類的testAnnotationConfig7()方法,輸出的結果信息如下所示。

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
personConfig2
io.mykit.spring.plugins.register.bean.Department
person
binghe001

可以看到,輸出結果中打印了io.mykit.spring.plugins.register.bean.Department,說明使用@Import導入bean時,id默認是組件的全類名。

@Import註解支持同時導入多個類,例如,我們再次創建一個Employee類,如下所示。

package io.mykit.spring.plugins.register.bean;
/**
 * @author binghe
 * @version 1.0.0
 * @description 測試@Import註解的bean
 */
public class Employee {
}

接下來,我們也將Employee類添加到@Import註解中,如下所示。

@Configuration
@Import({Department.class, Employee.class})
public class PersonConfig2 {

此時,我們再次運行SpringBeanTest類的testAnnotationConfig7()方法,輸出的結果信息如下所示。

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
personConfig2
io.mykit.spring.plugins.register.bean.Department
io.mykit.spring.plugins.register.bean.Employee
person
binghe001

可以看到,結果信息中同時輸出了io.mykit.spring.plugins.register.bean.Department和io.mykit.spring.plugins.register.bean.Employee,說明Department類的bean實例和Employee類的bean實例都導入到Spring容器中了。

好了,咱們今天就聊到這兒吧!別忘了給個在看和轉發,讓更多的人看到,一起學習一起進步!!

項目工程源碼已經提交到GitHub:https://github.com/sunshinelyz/spring-annotation

寫在最後

如果覺得文章對你有點幫助,請微信搜索並關注「 冰河技術 」微信公眾號,跟冰河學習Spring註解驅動開發。公眾號回復“spring註解”關鍵字,領取Spring註解驅動開發核心知識圖,讓Spring註解驅動開發不再迷茫。

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※為什麼 USB CONNECTOR 是電子產業重要的元件?

網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

※台北網頁設計公司全省服務真心推薦

※想知道最厲害的網頁設計公司"嚨底家"!

※推薦評價好的iphone維修中心