trying to access spring bean from guice module

╄→гoц情女王★ 提交于 2020-01-06 05:35:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!