咱们能够将一些bean组件交由Spring管理,而且Spring支持单实例bean和多实例bean。咱们本身写的类,能够经过包扫描+标注注解(@Controller、@Servcie、@Repository、@Component)的形式将其注册到IOC容器中,若是不是咱们本身写的类,好比,咱们在项目中引入了一些第三方的类库,此时,咱们须要将这些第三方类库中的类注册到Spring容器中,该怎么办呢?此时,咱们就可使用@Bean和@Import注解将这些类快速的导入Spring容器中。接下来,咱们来一块儿探讨下如何使用@Import注解给容器中快速导入一个组件。java
项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotationgit
向Spring容器中注册bean一般有如下几种方式:github
Spring 3.0以前,建立Bean能够经过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内。而在Spring 3.0以后提供了JavaConfig的方式,也就是将IOC容器里Bean的元信息以java代码的方式进行描述。咱们能够经过@Configuration与@Bean这两个注解配合使用来将原来配置在xml文件里的bean经过java代码的方式进行描述spring
@Import注解提供了@Bean注解的功能,同时还有xml配置文件里
先看一下@Import注解的源码:bash
@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注解的三种用法主要包括:测试
注意:咱们先来看第一种方法:直接填class数组的方式,其余的两种方式咱们后面继续讲。code
首先,咱们建立一个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实例。
咱们在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注解驱动开发再也不迷茫。