简体   繁体   English

从另一个jar添加Spring接口实现

[英]Add to spring an interface implementation from another jar

I have a " core " module which have an interface " DbService ". 我有一个“ 核心 ”模块,其接口为“ DbService ”。 The implementation of that interface is inside another module " MsSqlDbService " (for different purposes I have many implementation of " DbService ", so I just place "right" jar in the "right" place before running my program) 该接口的实现在另一个模块“ MsSqlDbService ”内(出于不同的目的,我有很多“ DbService ”实现,因此在运行程序之前,我只是将“正确的” jar放在“正确的”位置)

To load " DbService " I use standard java service loader java.util.ServiceLoader<DbService> 要加载“ DbService ”,我使用标准的Java服务加载器java.util.ServiceLoader<DbService>

So I wonder: is there a way to make my spring container manage " DbService "? 所以我想知道:有没有办法让我的spring容器管理“ DbService ”? Because now the spring container manages for me the class which actually loads implementation of " DbService ", instead of managing " DbService " itself. 因为现在spring容器为我管理实际加载“ DbService ”实现的类,而不是管理“ DbService ”本身。

(for different purposes I have many implementation of "DbService", so I just place "right" jar in the "right" place before running my program) (出于不同的目的,我有很多“ DbService”的实现,因此在运行程序之前,我只是将“正确的” jar放在“正确的”位置)

If I understand correctly, you have multiple JARs that all contain implementations of the same interface. 如果我理解正确,则您有多个JAR都包含同一接口的实现。 You want Spring to pickup whichever one is on the classpath at deployment time and then autowire everything together correctly? 您想让Spring在部署时拾取类路径中的任何一个,然后将所有东西正确地自动装配在一起?

Spring can definitely be used as a "poor man's plugin framework" to accomplish this sort of thing: 绝对可以将Spring用作完成此类任务的“穷人插件框架”:

1) In your main project, add this to your context: 1)在您的主项目中,将此添加到您的上下文中:

<import resource="classpath:applicationContext-dbService.xml"/>

2) In each JAR, create a file named applicationContext-dbService.xml . 2)在每个JAR中,创建一个名为applicationContext-dbService.xml的文件。 It would look something like this for the SQL Server example you provided. 对于您提供的SQL Server示例,它看起来像这样。

<bean id="msSqlDbService" class="com.foo.MsSqlDbServiceImpl"/>

3) When your main context is loaded, Spring will scan the classpath for files named applicationContext-dbService.xml and then process any beans that are defined in them. 3)加载主上下文后,Spring将在类路径中扫描名为applicationContext-dbService.xml文件,然后处理其中定义的所有bean。 Assuming that you only have one "plugin" JAR on the classpath at deployment time, you'll be able to autowire an instance of DbService into anything in your main project. 假设在部署时,类路径上只有一个“插件” JAR,则可以将DbService实例自动连接到主项目中的任何内容。

You can define the DbService interface as the member of the class. 您可以将DbService接口定义为DbService的成员。

class Demo{

@Autowired
@Qualifier("msSqlDbService")
private DbService dbService;

//Setter and getter
}

Now put the implementation classes(which you require) of DbService in spring xml config 现在将DbService的实现类(您需要的)放在spring xml config中

<bean id="msSqlDbService" class="xxxx.MsSqlDbService"/>//xxxx is the package name.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM