栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

@import用来导入@Configuration注解的配置类、声明@Bean注解的bean方法、导入importSelector的实现类或导入importBeanDefinitionRegistrar的实现类。

@import注解的作用

查看import注解源码


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
public @interface 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注解内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133954.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号