在 Spring 中将多个实现注入单个服务

编程入门 行业动态 更新时间:2024-10-24 10:26:34
本文介绍了在 Spring 中将多个实现注入单个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 DataSource 的服务接口和 1 个以上的实现,例如 XMLDataSource、DataBaseDataSource 等.

I have a servive interface called DataSource and more than 1 implementations like XMLDataSource, DataBaseDataSource etc.

我想根据一些用户交互向我的 Struts2 Action 注入(Spring)适当的实现,比如如果用户点击 XML 那么我需要使用 XML 实现.Spring已经用于DI框架.

I want to inject(Spring) appropriate implementation to my Struts2 Action based on some user interaction like if User clicks on XML then I need to use XML implementation. Spring has been used for DI framework.

@Autowired private DataSource dataSource;

请提出实现这一目标的最佳方法.

Please suggest what is best way to achieve this.

推荐答案

当使用 @Autowired 注释时,它是按类型自动装配的,您应该切换到按名称自动装配,这可以使用 @Qualifier 注释

When using the @Autowired annotation it's autowiring by type, you should switch to autowiring by name which can be done using the @Qualifier annotation

@Autowired @Qualifier("yourDataSource") private DataSource dataSource;

更多推荐

在 Spring 中将多个实现注入单个服务

本文发布于:2023-11-16 20:48:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   中将   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!