问题
I am using guice 4.2.2 in my Java application and need to utilize Spring beans (as part of a library I need to incorporate into my application). I have tried using SpringIntegration within guice and I am able to print the names of the beans loaded. However, when I try to access an object using injector.getInstance, the object I get is null.
Any thoughts on how I can go about troubleshooting this further? I can use Spring-projects's spring-guice integration if needed.
guice module
public class TestModule extends AbstractModule {
@override
protected void configure() {
final ClassPathXmlContext ctx = getApplicationcontext();
SpringIntegration.bindall(binder(), ctx.getBeanFactory());
bind(ApplicationContext.class.toInstance(ctx));
}
private Applicationcontext getApplicationcontext() {
final ClassLoader classLoader = this.getClass().getClassLoader();
final ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext();
ctx.setClassLoader(classLoader);
ctx.setConfiguration("test-context.xml");
ctx.refresh();
return ctx;
}
}
In a class (instead of using annotation), I am trying to get an instance as follows:
final TestClient client = injector.getInstance(TestClient.class);
I have tried using a class that will gt injected with the Spring bean using guice but when I access that bean, it appears to be null. Any thoughts?
来源:https://stackoverflow.com/questions/57962016/trying-to-access-spring-bean-from-guice-module