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

这篇文章主要讲解了Spring中@Import注解的作用和使用方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

张店网站建设公司创新互联,张店网站设计制作,有大型网站制作公司丰富经验。已为张店近千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的张店做网站的公司定做!

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

@Import注解的作用

查看Import注解源码

/**
 * Indicates one or more {@link Configuration @Configuration} classes to import.
 *
 * 

Provides functionality equivalent to the {@code } 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. * *

{@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. * *

May be declared at the class level or as a meta-annotation. * *

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(); }

分析类注释得出结论:

  • 声明一个bean
  • 导入@Configuration注解的配置类
  • 导入ImportSelector的实现类
  • 导入ImportBeanDefinitionRegistrar的实现类

@Import注解的使用

声明一个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 {
}

导入@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 {
}

导入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 {
}

导入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容器中。 

看完上述内容,是不是对Spring中@Import注解的作用和使用方法有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


当前名称:Spring中@Import注解的作用和使用方法
网址分享:http://bzwzjz.com/article/ppedhe.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆网站建设 成都网站设计 成都商城网站制作 定制网站建设 做网站设计 手机网站制作 温江网站设计 古蔺网站建设 成都网站制作 成都网站建设 网站建设 成都营销网站制作 成都网站建设公司 成都h5网站建设 网站设计公司 企业网站设计 手机网站建设 定制网站制作 成都网站建设 成都网站制作 高端定制网站设计 营销型网站建设