如何从已使用new()实例化的实例检索Eclipse Sisu创建的bean

时间:2016-10-05 16:24:57

标签: eclipse dependency-injection eclipse-plugin sisu

我正在尝试在我的Eclipse 3.x项目中集成一种“DI”。

所以我定义了一些像这样的“商业”bean:

@Named
class A{...}

@Named class B{ @Inject private A a;
public void doSmtg() { //use a
}

最后我有一个Eclipse AbstractHandler类(由plugin.xml扩展点注册),我想要注入B bean。

public class UIHandler extends AbstractHandler implements IHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
// I want to get my B bean where A is injected
}}

可行吗?我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

看看Sisu Activator,我使用了SisuTracker代码,现在使用该代码段来获取bean:

this.resourceToAssetFinder = (ILocator<R, Asset>) Activator.getPlugin().findLocator(Activator.getPlugin().getBundle().getBundleContext()).locate(Key.get(IResourceToAssetLocator.class)).iterator().next().getValue();

这样,所有bean都加载到我的bundle的类路径中。

辅助信息,引导Sisu所需的依赖关系是:

 <artifact>
    <id>com.google.guava:guava:${com.google.guava_guava.version}</id>
</artifact>
<artifact>
    <id>com.google.inject:guice:${com.google.inject_guice.version}</id>
    <transitive>false</transitive>
</artifact>
<artifact>
        <id>com.google.inject.extensions:guice-multibindings:${com.google.inject.extensions_guice-multibindings.version}</id>
        <transitive>false</transitive>
</artifact>
<artifact>      <id>org.eclipse.sisu:org.eclipse.sisu.inject:${org.eclipse.sisu.version}</id>
</artifact>