SpringBoot自动装配之@Import深入讲解
@Enable*底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中。而@Import提供4中用法:
- 导入Bean
- 导入配置类
- 导入ImportSelector 实现类。一般用于加载配置文件中的类
- 导入ImportBeanDefinitionRegistrar实现类。
1 导入Bean
定义一个user实体类:
package com.enable.entity; public class User { }
测试功能:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.UserConfig; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(User.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); User user = context.getBean(User.class); System.out.println(user); Map<String,User> map = context.getBeansOfType(User.class); System.out.println(map); } }
结果如下:
2 导入配置类
新建实体类:
package com.enable.entity; public class Role { }
package com.enable.entity; public class User { }
测试功能:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.UserConfig; import com.enable.entity.Role; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(UserConfig.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
测试结果:
3 导入ImportSelector 实现类
新建实体类:
package com.enable.entity; public class Role { }
package com.enable.entity; public class User { }
编写ImportSelector:
package com.enable.config; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{"com.enable.entity.Role","com.enable.entity.User"}; } }
测试功能:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.MyImportSelector; import com.enable.config.UserConfig; import com.enable.entity.Role; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(MyImportSelector.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
结果如下:
4 导入ImportBeanDefinitionRegistrar实现类
新建实体类:
package com.enable.entity; public class User { }
ImportBeanDefinitionRegistrar实现类
package com.enable.config; import com.enable.entity.User; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition(); registry.registerBeanDefinition("user",beanDefinition); } }
测试结果:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.MyImportBeanDefinitionRegistrar; import com.enable.config.MyImportSelector; import com.enable.config.UserConfig; import com.enable.entity.Role; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(MyImportBeanDefinitionRegistrar.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); Object user1 = context.getBean("user"); System.out.println(user1); User user = context.getBean(User.class); System.out.println(user); } }
结果如下:
到此这篇关于SpringBoot自动装配之@Import深入讲解的文章就介绍到这了,更多相关SpringBoot @Import内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
spring boot整合redis实现shiro的分布式session共享的方法
本篇文章主要介绍了spring boot整合redis实现shiro的分布式session共享的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-01-01SpringBoot + minio实现分片上传、秒传、续传功能
MinIO是一个基于Go实现的高性能、兼容S3协议的对象存储,使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构,这篇文章主要介绍了SpringBoot + minio实现分片上传、秒传、续传,需要的朋友可以参考下2023-06-06详解Java中Checked Exception与Runtime Exception 的区别
这篇文章主要介绍了详解Java中Checked Exception与Runtime Exception 的区别的相关资料,这里提供实例帮助大家学习理解这部分内容,需要的朋友可以参考下2017-08-08
最新评论