简体   繁体   English

从任何依赖注入容器获取类

[英]Get class from any Dependency injection container

I have 2 projects. 我有2个项目。 The first uses Spring 4 and the second uses jBoss 7. 第一个使用Spring 4,第二个使用jBoss 7。

I'm creating a dependency that given a Class name (like "com.foo.Bar") will get the instance from the injection context. 我正在创建一个给定类名称(例如“ com.foo.Bar”)的依赖项,该依赖项将从注入上下文中获取实例。

The classes will implement an interface (TransitionRule), so my method would be like: 这些类将实现一个接口(TransitionRule),所以我的方法将是:

public TransitionRule getRule(String className) {
    //... Magic goes here!
    //get the instance of "className" from any container that the imported project is using.
}

Can i do that with only one implementation or i will to create a different implementation to get from Spring and from Jboss context? 我可以仅使用一个实现来做到这一点,还是可以创建一个不同的实现来从Spring和Jboss上下文中获取?

You can do a bean lookup by name or type in Spring if you have access to the relevant Spring Context. 如果可以访问相关的Spring Context,则可以按名称或在Spring中进行键入来查找bean。

You can perform a JNDI lookup in a running JBoss container if the object has been registered in JNDI, for example, an EJB is generally registered in JNDI for you. 如果对象已在JNDI中注册,则可以在运行的JBoss容器中执行JNDI查找,例如,通常为您在JNDI中注册EJB。

Both of these methods follow the Lookup or Service-Locator pattern. 这两种方法都遵循Lookup或Service-Locator模式。 These are NOT injections. 这些不是注射剂。

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

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