[英]@Inject not working in Jersey REST webservices in Struts 2
我創建了一個用於測試 Struts 2 依賴項注入 ( @Inject
) 的應用程序。 除了我在其中定義了 webservices 操作的 Jersey REST 服務類之外,注入在許多領域都運行良好。
我收到如下所示的異常:
Sep 22, 2014 8:48:50 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at usermodules.services.UserModulesServices.userName(UserModulesServices.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
任何人都可以告訴我一些解決方案嗎?
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<bean class="net.viralpatel.struts2.action.MoreServiceImpl" name="services" />
<constant name="struts.action.excludePattern" value="/rest/.*" />
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<action name="login"
class="net.viralpatel.struts2.action.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
</package>
</struts>
UserModulesServices.java:
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import net.viralpatel.struts2.action.MoreServiceImpl;
import com.opensymphony.xwork2.inject.Inject;
@Path("/users")
public class UserModulesServices {
@Inject("services")
public MoreServiceImpl moreServiceImpl;
public MoreServiceImpl getMoreServiceImpl() {
return moreServiceImpl;
}
public void setMoreServiceImpl(MoreServiceImpl moreServiceImpl) {
this.moreServiceImpl = moreServiceImpl;
}
@GET
@Path("/name/{i}")
@Produces(MediaType.TEXT_PLAIN)
public String userName(@PathParam("i") String i) {
System.out.println("name::::::::" + moreServiceImpl.validate());
return "{\"name\":\"" + i + "\"}";
}
}
MoreServiceImpl.java:
package net.viralpatel.struts2.action;
public class MoreServiceImpl implements MoreServices{
@Override
public String validate() {
return "testing";
}
}
來自官方 CDI 插件文檔:
使用正確的
@Inject
Struts 2 及其核心組件 XWork 使用它自己的內部依賴注入容器。 有趣的是,您可以將其命名為 JSR-330 的祖母,因為它是曾經由 Crazybob Lee 開發的 Google Guice 的早期預發布版本 - 同一個 Bob Lee 與 SpringSource 的 Rod Johnson 一起領導 JSR-330 規范。
也就是說,您會發現
@Inject
注釋既是com.opensymphony.xwork2.inject.Inject
又是javax.inject.Inject
。 不要混淆這兩個 -javax.inject.Inject
是您想要與 Struts 2 CDI 插件和 CDI 集成一起使用的一般! 雖然您也可以使用 Struts 的內部注釋,但效果可能對未定義很奇怪 - 所以請檢查您的導入!
然后代替com.opensymphony.xwork2.inject.Inject
使用正確的: javax.inject.Inject
對於這個特定問題,您應該提供 bean 配置
<bean class="net.viralpatel.struts2.action.UserModulesServices" name="userModulesServices" />
注射會起作用,但它是供內部使用的,
在內部,該框架使用自己的依賴注入容器,這與 Google Guice 非常相似。
你應該考慮其他 DI 框架的機會。 請參閱依賴注入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.