Spring中@Import注解的作用和使用

@Import用来导入@Configuration注解的配置类、声明@Bean注解的bean方法、导入ImportSelector的实现类或导入ImportBeanDefinitionRegistrar的实现类。

@Import注解的作用

查看Import注解源码

/**
 * Indicates one or more {@link Configuration @Configuration} classes to import.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
 * Only supported for classes annotated with {@code @Configuration} or declaring at least
 * one {@link Bean @Bean} method, as well as {@link ImportSelector} and
 * {@link ImportBeanDefinitionRegistrar} implementations.
 *
 * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes
 * should be accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
 * injection. Either the bean itself can be autowired, or the configuration class instance
 * declaring the bean can be autowired. The latter approach allows for explicit,
 * IDE-friendly navigation between {@code @Configuration} class methods.
 *
 * <p>May be declared at the class level or as a meta-annotation.
 *
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use {@link ImportResource @ImportResource}
 *
 * @author Chris Beams
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportResource
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

	/**
	 * The @{@link Configuration}, {@link ImportSelector} and/or
	 * {@link ImportBeanDefinitionRegistrar} classes to import.
	 */
	Class<?>[] value();
}

分析类注释得出结论:

  1. 声明一个bean
  2. 导入@Configuration注解的配置类
  3. 导入ImportSelector的实现类
  4. 导入ImportBeanDefinitionRegistrar的实现类
@Import注解的使用
  1. 声明一个bean

    package com.example.demo.bean;
    
    public class TestBean1 {
    }
    
    package com.example.demo;
    
    import com.example.demo.bean.TestBean1;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({TestBean1.class})
    @Configuration
    public class AppConfig {
    }
    
  2. 导入@Configuration注解的配置类

    package com.example.demo.bean;
    
    public class TestBean2 {
    }
    
    package com.example.demo.bean;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class TestConfig {
        @Bean
        public TestBean2 getTestBean2(){
            return new TestBean2();
        }
    }
    
    package com.example.demo;
    
    import com.example.demo.bean.TestBean1;
    import com.example.demo.bean.TestConfig;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({TestBean1.class,TestConfig.class})
    @Configuration
    public class AppConfig {
    }
    
    
  3. 导入ImportSelector的实现类

    package com.example.demo.bean;
    
    public class TestBean3 {
    }
    
    package com.example.demo.bean;
    
    import org.springframework.context.annotation.ImportSelector;
    import org.springframework.core.type.AnnotationMetadata;
    
    public class TestImportSelector implements ImportSelector {
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[]{"com.example.demo.bean.TestBean3"};
        }
    }
    
    package com.example.demo;
    
    import com.example.demo.bean.TestBean1;
    import com.example.demo.bean.TestConfig;
    import com.example.demo.bean.TestImportSelector;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({TestBean1.class,TestConfig.class,TestImportSelector.class})
    @Configuration
    public class AppConfig {
    }
    
  4. 导入ImportBeanDefinitionRegistrar的实现类

    package com.example.demo.bean;
    
    public class TestBean4 {
    }
    
    package com.example.demo.bean;
    
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.beans.factory.support.RootBeanDefinition;
    import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
    import org.springframework.core.type.AnnotationMetadata;
    
    public class TestImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(TestBean4.class);
            registry.registerBeanDefinition("TestBean4", rootBeanDefinition);
        }
    }
    
    package com.example.demo;
    
    import com.example.demo.bean.TestBean1;
    import com.example.demo.bean.TestConfig;
    import com.example.demo.bean.TestImportBeanDefinitionRegistrar;
    import com.example.demo.bean.TestImportSelector;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({TestBean1.class,TestConfig.class,TestImportSelector.class,TestImportBeanDefinitionRegistrar.class})
    @Configuration
    public class AppConfig {
    }
    

最后,我们来看下导入结果:


package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void test() {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
        System.out.println("--------------------------------------------------------");
        for (String beanDefinitionName: beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
        System.out.println("--------------------------------------------------------");
    }

}

打印结果如下:

--------------------------------------------------------
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
appConfig
com.example.demo.bean.TestBean1
com.example.demo.bean.TestConfig
getTestBean2
com.example.demo.bean.TestBean3
TestBean4
--------------------------------------------------------

可以看出TestBean1,TestBean2,TestBean3,TestBean4通过不同的4种导入方法被导入SpringIOC容器中。

  • 19
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: @Import注解Spring框架的一种注解,可以用来快速导入多个组件,包括类、包或者配置类。 使用@Import注解可以在一个配置类快速导入其他组件,而不必通过@Bean或@ComponentScan注解来导入。这样可以方便地将功能模块化,并且使用起来也更加方便。 使用方法如下: 1. 在配置类上使用@Import注解,并指定要导入的组件的类型数组。 例如: ``` @Import({MyConfiguration.class, MyBean.class}) public class AppConfig { // ... } ``` 2. 也可以使用ImportSelector接口和ImportBeanDefinitionRegistrar接口来动态选择和注册组件。 例如: ``` @Import(MyImportSelector.class) public class AppConfig { // ... } public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { // 在这里可以动态的返回需要导入的组件 return new String[] {MyBean.class.getName()}; } } ``` 使用@Import注解导入组件的好处是可以方便地将组件模块化,可以将一个功能模块分成若干个配置类,然后使用@Import注解导入。这样可以让代码更加清晰,更加方便维护。 ### 回答2: @Import注解Spring框架的一种注解,在使用注解时,可以将其他配置类或者Bean定义类导入到当前配置类。通过@Import注解,可以在一个配置类同时引入多个不同的配置类或者Bean定义类,实现了配置的模块化和复用。 @Import注解可以实现多种功能,具体包括以下几点: 1. 导入配置类:通过@Import注解可以将其他的配置类导入到当前的配置类,这样可以将多个配置类组合在一起,实现配置的分离和复用,提高代码的可维护性和可读性。 2. 导入Bean定义类:除了配置类之外,@Import注解也可以将其他的Bean定义类引入到当前配置类,这样可以将多个不同的Bean定义类组合在一起,实现Bean的组装和扩展。 3. 导入自动配置类:Spring Boot框架的自动配置就是通过@Import注解来实现的,通过将相应的自动配置类导入到配置类,可以实现对应功能的自动配置和初始化,减少了开发人员的工作量。 4. 导入条件配置:通过@Import注解可以根据不同的条件来选择性地导入不同的配置类或者Bean定义类,根据具体的条件来进行动态的选择和配置,实现更加灵活和可配置化的开发。 总结来说,@Import注解Spring框架起到了组合和扩展配置的作用,可以将多个配置类或者Bean定义类导入到当前配置类,实现了配置的模块化和复用,同时也提供了条件导入的功能,使得配置的选择更加灵活和可配置化。 ### 回答3: @Import注解Spring框架的一个注解作用是用于导入其他的配置类或者Bean。通过@Import注解,我们可以将其他的配置类或者Bean引入到当前的配置类,从而实现配置类之间的解耦。 @Import注解可以用于导入其他的@Configuration配置类,这样可以方便地将多个配置类合并到一个总的配置类。通过@Import注解,我们可以将不同的配置类按照逻辑进行划分,分别编写配置类,然后使用@Import注解将它们引入到一个总的配置类。这样做的好处是,能够更好地组织和管理配置类,提高代码的可读性和可维护性。 除了导入其他的配置类,@Import注解还可以用于导入其他的普通的Bean。这对于一些无法通过@Configuration注解进行配置的Bean来说非常有用。通过@Import注解,我们可以将这些Bean引入到当前的配置类,然后使用@Autowired注解进行注入。 总之,@Import注解作用是使得配置类之间可以进行解耦,并且可以方便地引入其他的配置类或者Bean。它在Spring框架的应用非常广泛,能够有效地提高代码的可读性、可维护性和复用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值