Spring中@Import注解如何实现组件自动导入及使用技巧?

2026-05-26 04:031阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1174个文字,预计阅读时间需要5分钟。

Spring中@Import注解如何实现组件自动导入及使用技巧?

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

`@Import` 注解的作用:- 导入配置类,这些类包含 Spring 容器配置信息。- 导入由 `@Bean` 注解标记的方法,用于创建和注册 Bean。- 导入实现 `ImportSelector` 接口的类,用于动态选择要导入的组件。- 导入实现 `ImportBeanDefinitionRegistrar` 接口的类,用于在注册 Bean 定义之前执行额外操作。

`@Import` 注解的用法示例:java@Import({MyConfig.class, MyBeanMethod.class, MyImportSelector.class, MyBeanDefinitionRegistrar.class})

`@Import` 的作用:- 简化 Spring 容器配置。- 动态地管理 Bean 的创建和注册。

查看 `@Import` 源码:java/** * Indicates that the annotated component class defines one or more import statements. * The import statements are processed by the Spring container as follows: * * Imported bean definitions are registered with the registry if they are of type * BeanDefinition. * Imported configuration classes are processed by Spring's ConfigurationClassPostProcessor. * This includes processing @Bean methods defined within the imported classes, * as well as handling other aspects of the configuration (e.g., resolving placeholder values, etc.). * *

By default, this annotation will include all static nested classes of the annotated component * that are annotated with @Configuration as import classes. Alternatively, it can * include classes that are explicitly listed. * * @author Rod Johnson * @since 3.1 */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Import { /** * Returns the classes to be imported. */ Class[] value() default {};

/** * Returns the class names to be imported. * @since 4.0 */ String[] classnames() default {};}

Spring中@Import注解如何实现组件自动导入及使用技巧?

注意:- 使用 `@Import` 时,不需要手动创建和注册 Bean。- `@Import` 的使用可以简化 Spring 容器配置,提高代码的可读性和可维护性。

@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注解内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:作用

本文共计1174个文字,预计阅读时间需要5分钟。

Spring中@Import注解如何实现组件自动导入及使用技巧?

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

`@Import` 注解的作用:- 导入配置类,这些类包含 Spring 容器配置信息。- 导入由 `@Bean` 注解标记的方法,用于创建和注册 Bean。- 导入实现 `ImportSelector` 接口的类,用于动态选择要导入的组件。- 导入实现 `ImportBeanDefinitionRegistrar` 接口的类,用于在注册 Bean 定义之前执行额外操作。

`@Import` 注解的用法示例:java@Import({MyConfig.class, MyBeanMethod.class, MyImportSelector.class, MyBeanDefinitionRegistrar.class})

`@Import` 的作用:- 简化 Spring 容器配置。- 动态地管理 Bean 的创建和注册。

查看 `@Import` 源码:java/** * Indicates that the annotated component class defines one or more import statements. * The import statements are processed by the Spring container as follows: * * Imported bean definitions are registered with the registry if they are of type * BeanDefinition. * Imported configuration classes are processed by Spring's ConfigurationClassPostProcessor. * This includes processing @Bean methods defined within the imported classes, * as well as handling other aspects of the configuration (e.g., resolving placeholder values, etc.). * *

By default, this annotation will include all static nested classes of the annotated component * that are annotated with @Configuration as import classes. Alternatively, it can * include classes that are explicitly listed. * * @author Rod Johnson * @since 3.1 */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Import { /** * Returns the classes to be imported. */ Class[] value() default {};

/** * Returns the class names to be imported. * @since 4.0 */ String[] classnames() default {};}

Spring中@Import注解如何实现组件自动导入及使用技巧?

注意:- 使用 `@Import` 时,不需要手动创建和注册 Bean。- `@Import` 的使用可以简化 Spring 容器配置,提高代码的可读性和可维护性。

@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注解内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:作用