天天看點

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

【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()方法,輸出的結果資訊如下所示。

io.mykit.spring.plugins.register.bean.Department

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

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

public class Employee {

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

@Import({Department.class, Employee.class})

io.mykit.spring.plugins.register.bean.Employee

可以看到,結果資訊中同時輸出了io.mykit.spring.plugins.register.bean.Department和io.mykit.spring.plugins.register.bean.Employee,說明Department類的bean執行個體和Employee類的bean執行個體都導入到Spring容器中了。

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

原文位址

https://www.cnblogs.com/binghe001/p/13089993.html