Guice + Jersey集成注入空对象 [英] Guice + Jersey integration injects null objects
问题描述
继在AppEngine上运行Maven,Jersey和Guice - 第3部分,除了将对象注入Jersey POJO。
我从这个配置唯一的区别是我也有Objectify集成,但是这是工作。
注入到
HelloWorldServlet
中的 TestClass
实例(单例)可以工作,但 TestClass 和 SecondTest ( RequestScoped
)注入 HeyResource
POJO总是 null
。
我猜想HK2和Guice之间的互动是怪罪于此,但这是我第一次与Guice 和 Jersey 和 HK2合作,所以我都在海上。
我的配置是:
- 平台:Win 7
- GAE SDK 1.9.26
- Jave 1.7.0_79
- Jersey:2.5.1
- Guice:4.0
- 物化:5.1.7
- HK2 Guice桥:2.2.0
- Maven 3.3.3
<使用Jersey 2,你不需要使用像Jersey 1那样需要的Guice web包装器。你已经有了 guice-bridge
,你只需要在Jersey配置中使用HK2进行配置。请参阅 Guice / HK2 Bridge 。
你基本上需要得到HK2的 ServiceLocator
的句柄来绑定这两个框架。 Jersey允许您在应用程序的多个位置注入定位器。最需要它的地方是配置类(即 ResourceConfig
)。
公共类JerseyConfig扩展了ResourceConfig {
@注入
public JerseyConfig(ServiceLocator locator){
packages(your.packages.to.scan);
GuiceBridge.getGuiceBridge()。initializeGuiceBridge(locator);
//添加你的Guice模块。
注射器注射器= Guice.createInjector(new GuiceModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
$ b如果您使用web.xml来配置应用程序,您可以使用init-param将此类添加到您的配置中 < servlet>
< servlet-name> Jersey Web Application< / servlet-name>
< servlet-class> org.glassfish.jersey.servlet.ServletContainer< / servlet-class>
< init-param>
< param-name> javax.ws.rs.Application< / param-name>
< param-value> com.stackoverflow.jersey.JerseyConfig< / param-value>
< / init-param>
1< / load-on-startup>
< / servlet>
Following the excellent step-by-step given in Up and running on AppEngine with Maven, Jersey and Guice - Part 3, I have been able to get everything working except injecting objects into a Jersey POJO.
The only difference I have from that configuration is that I also have Objectify integrated, but that is working.
The TestClass
instance (a singleton) injected into HelloWorldServlet
works, but the TestClass
and SecondTest
(RequestScoped
) objects injected into the HeyResource
POJO are always null
.
I suspect the the interaction between HK2 and Guice is to blame here, but this is my first project with Guice and Jersey and HK2, so I am all at sea.
My configuration is:
- Platform: Win 7
- GAE SDK 1.9.26
- Jave 1.7.0_79
- Jersey: 2.5.1
- Guice: 4.0
- Objectify: 5.1.7
- HK2 Guice-bridge: 2.2.0
- Maven 3.3.3
解决方案 With Jersey 2 you don't need to use the Guice web wrapper like was needed with Jersey 1. You already have the guice-bridge
, you just need to configure it with HK2 within the Jersey configuration. See The Guice/HK2 Bridge.
You basically need to get a handle on the HK2's ServiceLocator
to bind the two frameworks. Jersey allows you to inject the locator in many locations of the application. The place where you would need it most is in the configuration class (i.e. the ResourceConfig
). Here is an example of how you can configure it.
public class JerseyConfig extends ResourceConfig {
@Inject
public JerseyConfig(ServiceLocator locator) {
packages("your.packages.to.scan");
GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
// add your Guice modules.
Injector injector = Guice.createInjector(new GuiceModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
}
}
If you are using web.xml to configure the app, you can add this class to your configuration with an init-param
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.stackoverflow.jersey.JerseyConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这篇关于Guice + Jersey集成注入空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!