Spring常用注解@Import的三种使用方式,你都了解了吗?

开发 前端
在Spring4.2之前的版本中被导入的类必须是配置类也就是类上有@Configuration注解,4.2后的版本随意一个普通类也可以。

环境:SpringBoot2.6.12

三种方法说明

  1. 直接导入普通数组的方式
  2. 导入ImportSelector类方式
  3. 导入ImportBeanDefinitionRegistrar方式

被导入的类加入到Spring IOC容器中。

直接导入普通数组的方式

public class A {
  @Bean
  public B b() {
    return new B() ;
  }

在配置类中导入上面的类

@Import({A.class})
@Configuration
public class ImportConfig {
}

这样A,B都被Spring IOC容器管理了。这里可以导入多个类。

注意:在Spring4.2之前的版本中被导入的类必须是配置类也就是类上有@Configuration注解,4.2后的版本随意一个普通类也可以。

测试

@Resource
private A a ;
@Resource
private B b ;
  
@Test
public void testImport() {
  System.out.println("a = " + a) ;
  System.out.println("b = " + b) ;
}

图片图片

没问题,能正常的输出。

注意,请看下面的示例:

public class ImportMain {


  static class Person{
  }
  
  static class A{
    @Bean
    public Person p() {
      Person person = new Person();
      System.out.println("p(), " + person) ;
      return person ;
    }
    @Bean
    public Date d() {
      Person p = p() ;
      System.out.println("d(), " + p) ;
      return new Date() ;
    }
  }
  @Import({A.class})
  @Configuration
  static class AppConfig {
  }
}

在上面的示例中在d()方法中调用p()方法,那这里得到的Person对象和p()方法中定义的是同一个吗?

在上面的代码中一定不是同一个,输出如下:

p(), com.pack.main.importaware.ImportMain$Person@18dfcc1
p(), com.pack.main.importaware.ImportMain$Person@19f7dcf
d(), com.pack.main.importaware.ImportMain$Person@19f7dcf

分别是两个不同的对象,如何解决呢?我们只需要在A类上添加@Configuration即可,添加了该类后Spring容器首先会对当前的A这个类创建代理,当我们在A这个类中调用其它方法的时候会通过拦截器BeanMethodInterceptor进行拦截;在该拦截器中会根据你调用的方法来确定对应的beanName,然后在容器中查找是否有对应的Bean,如果有则直接返回,所以就确保了在当前类中你不管调用多少次其它@Bean方法都能保证是同一个对象。

导入ImportSelector类方式

通过实现ImportSelector接口

public class E {
  @Bean
  public G g() {
    return new G() ;
  }
}
public class F {
}
public class G {
}

配置类

@Import({C.class, A.class})
@Configuration
public class ImportConfig {
}

实现ImportSelector接口

public class C implements ImportSelector {


  @Override
  public String[] selectImports(AnnotationMetadata importingClassMetadata) {
    return new String[] {"com.pack.import_annotation.E", "com.pack.import_annotation.F"};
  }


}

这里的返回值中必须是完整的包名+类名

注意:这里可以返回空的String数组(length = 0),但是不能返回null。实现ImportSelector该接口的这个本身是不会被注册为Bean的。

测试

@Resource
private E e ;
@Resource
private F f ;
@Resource
private G g ;
  
@Test
public void testImport() {
  System.out.println("e = " + e) ;
  System.out.println("f = " + f) ;
  System.out.println("g = " + g) ;
}

图片图片

导入ImportBeanDefinitionRegistrar方式

public class H implements ImportBeanDefinitionRegistrar {


  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition() ;
    beanDefinition.setBeanClass(X.class) ;
    // 给X这个类配置属性name值
    beanDefinition.getPropertyValues().addPropertyValue("name", "张三") ;
    // x 为当前X类在Spring容器中的beanName
    registry.registerBeanDefinition("x", beanDefinition) ;
  }


}

配置类

public class X {
  
  private String name ;


  public String getName() {
    return name;
  }


  public void setName(String name) {
    this.name = name;
  }
  
}
@Import({C.class, A.class, H.class})
@Configuration
public class ImportConfig {
}

测试:

图片图片

注意:实现ImportBeanDefinitionRegistrar该接口本身这个类是不会被注册为Bean的。

完毕!!!

责任编辑:武晓燕 来源: Spring全家桶实战案例源码
相关推荐

2022-07-18 09:41:29

属性类型安全Spring

2024-02-19 08:40:22

2023-06-26 07:32:43

Kubernetes容器

2023-04-28 12:37:59

Spring@Bean使用方式

2023-04-23 09:50:50

@BeanSpring

2012-07-17 09:16:16

SpringSSH

2023-11-06 17:37:17

技术架构任务队列

2023-11-27 00:48:46

displayvisibility

2024-03-20 08:31:40

KotlinExtension计算

2023-12-14 12:55:41

Pythondel语句

2022-10-24 08:45:23

数据库应用场景区块链

2024-01-03 08:08:51

Pulsar版本数据

2023-09-27 16:29:55

开发团队信息

2023-05-09 09:00:20

版本Canary框架

2011-06-03 11:53:06

Spring接口

2009-07-20 15:08:41

Spring实例化Be

2023-10-12 16:39:00

2023-10-26 07:09:30

Golangrune字符

2021-11-26 11:07:14

cowsay命令Linux

2020-08-06 11:05:30

函数调用寄存器语言
点赞
收藏

51CTO技术栈公众号