Google Guice 3和OSGi(Eclipse春分)问题,
我在 OSGi 容器中运行 Guice 3 时遇到问题。 以下是我编写的一个简单测试,用于测试 Guice 是否能与 OSGi 配合良好。
一个简单的 guice 模块,如:
public class Module extends AbstractModule {
@Override
protected void configure() {
bind(IInterface.class).to(IImplement.class);
}
}
IInterface 和 IImplement 都非常简单。
OSGi 激活器如下所示:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Injector inj = Guice.createInjector(new Module());
IInterface e = inj.getInstance(IInterface.class);
e.sayHello();
}
在 Eclipse 中,我创建了一个包含所有 Guice Jars 的目标,并且为了使 guice 自行解析,我为 aopalliance.jar 和 javax.injector.jar 创建了两个附加包
但是,这个简单的测试无法加载测试包给我错误消息抱怨找不到 guice 类找不到:
Exception in guicetest.Activator.start() of bundle guicetest
Caused by: java.lang.NoClassDefFoundError: com/google/inject/binder/AnnotatedBindingBuilder
at guicetest.guice.Module.configure(Module.java:11)
我希望我已经把问题说清楚了。谁能告诉我如何解决这个问题?
I have trouble running Guice 3 within an OSGi container.
Following is a simple test I wrote to test if Guice work well with OSGi.
A simple guice module like:
public class Module extends AbstractModule {
@Override
protected void configure() {
bind(IInterface.class).to(IImplement.class);
}
}
The IInterface and IImplement are both very trivial.
The OSGi activator like this:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Injector inj = Guice.createInjector(new Module());
IInterface e = inj.getInstance(IInterface.class);
e.sayHello();
}
In Eclipse, I made a target contains all the Guice Jars, and to make guice resolve itself, I made two additional bundle for the aopalliance.jar and javax.injector.jar
However, this simple test fail to load the test bundle, gives me error message complaining cannot find a guice class cannot be found:
Exception in guicetest.Activator.start() of bundle guicetest
Caused by: java.lang.NoClassDefFoundError: com/google/inject/binder/AnnotatedBindingBuilder
at guicetest.guice.Module.configure(Module.java:11)
I hope I have made the problem clear. Can anyone show me how to resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊,刚刚发布问题后我找到了问题的根源。我没有在测试包的导入包中指定有问题的类所在的 com.google.inject.binder 包。尽管模块不直接导入该包,但看起来仍然有必要指定所有间接依赖的包。
Ah, after just posting the question I found the root of the problem. I didn't specify the com.google.inject.binder package, which the problematical class resides, in the test bundle's Import-Packages. Although the Module doesn't import directly that package, it looks it is still necessary to specify all the indirect dependent packages as well.