spring如何动态指定具体实现类
作者:登顶 时间:2022-04-13 07:52:21
在写接口实现时,有时会有多个实现类。这篇文章介绍在调用时通过传入字符串来指定具体的实现类。
一.接口与实现类:
// 接口
public interface ServiceInterface {
public void method();
}
// 具体两个实现类
@Service("aService")
public class AServiceImpl implements ServiceInterface {
@Override
public void method() {
System.out.println("the impl is A");
}
@Override
public String toString() {
return "A";
}
}
@Service("bService")
public class BServiceImpl implements ServiceInterface {
@Override
public void method() {
System.out.println("the impl is B");
}
@Override
public String toString() {
return "B";
}
}
在实现类中重写了toString() 方法,可以自定义字符串,当调用时传入指定的字符串就能获取到相应的bean。
二.register书写:
@Service("register")
public class Register implements InitializingBean, ApplicationContextAware {
private Map<String, ServiceInterface> serviceImplMap = new HashMap<>();
private ApplicationContext applicationContext;
// 获取spring的上下文
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
// 获取接口实现类的所有bean,并按自己定的规则放入map中
@Override
public void afterPropertiesSet() throws Exception {
Map<String, ServiceInterface> beanMap = applicationContext.getBeansOfType(ServiceInterface.class);
// 以下代码是将bean按照自己定的规则放入map中,这里我的规则是key:service.toString();value:bean
// 调用时,参数传入service.toString()的具体字符串就能获取到相应的bean
// 此处也可以不做以下的操作,直接使用beanMap,在调用时,传入bean的名称
for (ServiceInterface serviceImpl : beanMap.values()) {
serviceImplMap.put(serviceImpl.toString(), serviceImpl);
}
}
public ServiceInterface getServiceImpl(String name) {
return serviceImplMap.get(name);
}
}
三.测试类:
@Resource
Register register;
@Test
public void testService() {
ServiceInterface service = register.getServiceImpl("A");
service.method();
ServiceInterface service2 = register.getServiceImpl("B");
service2.method();
}
运行结果,如图:
备注:
在spring加载后,获取applicationContext的方法:
实现ApplicationContextAware接口的Bean,在Bean加载的过程中可以获取到Spring的ApplicationContext,这个尤其重要,ApplicationContext是Spring应用上下文,从ApplicationContext中可以获取包括任意的Bean在内的大量Spring容器内容和信息
@Component("informerRegistry")
public final class InformerRegistry implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
关于spring常用bean扩展接口可参考:http://www.cnblogs.com/xrq730/p/5721366.html
注意:
使用以下方法获取spring上下文时,会启动spring。多次写以下方法,就会启动多个spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:META-INF/spring/*.xml");
标签:spring,实现类
0
投稿
猜你喜欢
springboot docker jenkins 自动化部署并上传镜像的步骤详解
2023-07-28 01:54:38
Java图片裁剪和生成缩略图的实例方法
2023-11-09 00:01:50
Unity打开淘宝app并跳转到商品页面功能的实现方法
2023-06-17 01:05:18
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
java导出大批量(百万以上)数据的excel文件
2023-11-16 13:13:22
JavaApi实现更新删除及读取节点
2023-11-10 07:30:33
聊聊@RequestBody和Json之间的关系
2023-11-27 03:31:45
详解Java类型擦除机制
2023-10-29 06:41:21
Java如何根据不同系统动态获取换行符和盘分割符
2022-02-27 10:33:10
Struts2学习笔记(9)-Result配置全局结果集
2022-04-09 11:33:10
obix协议在java中的配置和使用详解
2023-11-25 20:59:42
自定义类加载器以及打破双亲委派模型解析
2023-06-22 22:03:59
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
MyBatis-Plus联表查询以及分页代码实例
2023-11-26 01:51:32
Spring实战之SpEl语法实例详解
2023-09-18 07:56:03
JavaFX实现简单日历效果
2023-05-16 08:43:30
如何把char数组转换成String
2023-11-11 07:38:15
解决微服务feign调用添加token的问题
2023-09-11 09:38:05
Java基于Socket实现网络编程实例详解
2023-11-23 12:22:37
Java全面分析面向对象之继承
2023-11-23 11:55:59