spring @import注解使用场景
一、SpringBoot 的 @Import 用于将指定的类实例注入之Spring IOC Container中。 SpringBoot 提供了 三种使用 @Import 将 类实例注入至 Spring IOC Container中 的实例。
二.直接注入
直接引入要注入的类即可。
package com.tpw.newday.service.people;import com.tpw.newday.bean.PeopleBean;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;/*** <h3>newday</h3>* <p></p>** @author : lipengyao* @date : 2021-06-25 14:08:52**/
@Scope("singleton")
@Service(value = "MsgSendServiceImpl")
//@Import({PeopleServiceImpl.class}) //直接注入
//@Import({PeopleImportSeletor.class}) //实现 ImportSelector 注入
@Import({PeopleImportBeanDefineRegister.class}) //实现 ImportBeanDefinitionRegistrar 接口 注入
public class MsgSendServiceImpl implements MsgSendService {// @Autowired
// private IPeopleService peopleService;@Value("${people.beanfactory}")private String peopleBeanFactory;@Bean(name = {"MsgPeopleBean"},autowire = Autowire.NO)public PeopleBean msgPeopleBean(){System.out.println(" MsgSendServiceImpl -->msgPeopleBean peopleBeanFactory: " + peopleBeanFactory);return new PeopleBean("msg People");}@Bean(name = {"people1"},autowire = Autowire.NO)public PeopleBean peopleBean1(){return new PeopleBean("zhangsan");}@Overridepublic boolean sendMsg(String msg) {System.out.println("MsgSendServiceImpl-->sendMsg msg:" + msg);return true;}@Overridepublic PeopleBean getMsgPeopleBean() {return null;}
}
三.实现 ImportSelector 注入
根据此接口的实现体动态选择注册哪些类到容器。
package com.tpw.newday.service.people;import cn.hutool.core.util.StrUtil;
import com.tpw.newday.utils.PropertiesUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;import java.util.Properties;/*** <h3>newday</h3>* <p></p>** @author : lipengyao* @date : 2021-06-30 15:10:06**/
@Component
public class PeopleImportSeletor implements ImportSelector{@Value("${people.beanfactory}")private String peopleBeanFactory;@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {Properties properties = PropertiesUtils.getProperties("application-dev.properties");peopleBeanFactory = properties.getProperty("people.beanfactory");System.out.println(" PeopleImportSeletor -->selectImports peopleBeanFactory: " + peopleBeanFactory);if (StrUtil.equals(peopleBeanFactory,"PeopleSpringFactory" )){return new String[]{PeopleSpringFactory.class.getName()};}else{return new String[]{PeopleBeanFactory.class.getName()};}}
}
四.实现 ImportBeanDefinitionRegistrar 接口 注入
可以手动将外部的任何类BEAN注册到容器工厂中。
package com.tpw.newday.service.people;import org.springframework.beans.factory.config.BeanDefinition;
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;/*** <h3>newday</h3>* <p></p>** @author : lipengyao* @date : 2021-07-01 10:42:13**/
public class PeopleImportBeanDefineRegister implements ImportBeanDefinitionRegistrar{@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {Class[] classes = {PeopleBeanFactory.class,PeopleSpringFactory.class};BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(classes[0]);registry.registerBeanDefinition("PeopleBeanFactory", beanDefinitionBuilder.getBeanDefinition());BeanDefinitionBuilder beanDefinitionBuilder1 = BeanDefinitionBuilder.rootBeanDefinition(classes[1]);registry.registerBeanDefinition("PeopleSpringFactory", beanDefinitionBuilder1.getBeanDefinition());}
}
spring @import注解使用场景相关推荐
- Spring @Import注解配置类方法内部调用没有注入属性值的坑
一.场景复现 application.yaml spring:application:name: config-testprofiles:active: devconfig:config-01:nam ...
- Spring @Import 注解使用详解
文章目录 1. @Import 作用 2. 方式一:@Import 直接导入 3. 方式二:ImportSelector 接口 4. 方式三:ImportBeanDefinitionRegistrar ...
- Spring@Import注解的三种用法
参考:https://www.jianshu.com/p/7eb0c2b214a7 转载于:https://www.cnblogs.com/2YSP/p/10963698.html
- Spring高级之注解@Import注解、ImportSelector、ImportBeanDefinitionRegistrar详解(超详细)
定义/作用 @Import注解只能作用在类上,一种使用场景是在spring注解驱动开发环境下与配置类配合使用的,其作用是引用其他配置类.使得我们可以和早起的基于XML配置文件开发那样.使用不同的配置类 ...
- 五、Spring中的@Import注解
一.使用@Import注解导入组件 @Import注解的作用是给容器中导入组件,回顾下我们给容器中导入组件的方式,可以通过Spring的xm配置方式,可以通过注解,如@Component等,也可以通过 ...
- java的 import注解_[java]一分钟学会spring注解之@Import注解
今天主要从以下几方面来介绍一下@Import注解 @Import注解是什么 @Import的三种使用方式 1,@Import注解是什么 通过导入的方式实现把实例加入springIOC容器中 2,@Im ...
- spring注解之@Import注解的三种使用方式
1.@Import注解须知 1.@Import只能用在类上 ,@Import通过快速导入的方式实现把实例加入spring的IOC容器中 2.加入IOC容器的方式有很多种,@Import注解就相对很牛皮 ...
- Spring注解驱动开发第10讲——在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean
写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,不仅可以使用@Import注解快速向容器中导入bean,也可以在@Import注解中使用ImportSe ...
- 火眼金睛,看透Spring处理Import注解的全过程
文章目录 一.前言 二.Enable前缀的注解上面有@Import注解 三.常见的四种Import注解用法(根据类Abc的不同类型) 四.Spring处理@Import注解的过程(全文重点,对应四种@ ...
最新文章
- 今天,苹果遭遇大宕机
- 技术贴 | MetaboAnalyst 4.0,代谢组学研究利器的升级
- 教你加快Win7 的启动速度
- include element into ABAP word document
- java finally在return_Java finally语句到底是在return之前还是之后执行?
- Python中将array类型不按科学计数法存在文件中的方法
- java监听剪贴板_在java中实现windows剪贴板监视
- 设置php中字符编码_php如何设置字符编码
- 如何下载全国的POI数据,如何获取全国的POI数据,poi数据搜索,高德poi获取,poi数据分析,poi免费数据,城市规划数据
- 用 tf.data 加载图片
- 接口压力测试:Jmeter【专门做接口压力测试】
- ios重签名工具ios-app-signer的使用
- Linux VGA驱动移植实验
- Graph Convolutional Networks Meet Markov Random Fields: Semi-Supervised Community Detection in Attri
- Git develop分支的一些操作
- 使用Tracup中的时间阻塞,提升工作效率(完整指南)
- 解剖点击量过亿的软文牛人是怎样炼成的?
- HDU6348 Buy and Resell
- 机械硬盘显示拒绝访问要怎样办啊
- 【Shotcut】开源免费视频编辑软件 - 微信视频编辑利器
热门文章
- 致远OA如何实施才可行
- 房价集体上扬?最新房价数据分析看房价走势
- “下沉市场”+“内容生态”,OTA的两道救命题?
- 从Q3财报看百度营销成长
- java实现图片对比功能_Java 照片对比功能的实现
- matlab曲线拟合法,MATLAB曲线拟合
- C语言程序运行慢是什么问题,为什么cgo的表现如此缓慢?我的测试代码有什么问题吗?...
- java 拦截器响应中取所有参数,spring boot拦截器中获取request post请求中的参数
- 后面的参数_常用的JVM参数,你现在就记好
- java的classpath是什么_JAVA初学者classpath设置情况是什么?