欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-09 19:01:52
@import用来导入@configuration注解的配置类、声明@bean注解的bean方法、导入importselector的实现类或导入importbeandefinitionregistra...

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

分析类注释得出结论:

  • 声明一个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注解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!