2014-09-22 55 views
2

我已經創建了一個用於測試struts2依賴注入(@Inject)的應用程序。注射在很多方面都很好,除了我定義了網絡服務操作的球衣休息服務課程之外。Struts2 @Inject不能在澤西島工作其他webservices

我越來越喜歡如下圖所示

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) 

誰能告訴我一些解決方案,這

struts2.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"; 
    } 

} 
+0

閱讀評論[此問題](http://stackoverflow.com/q/17244036/1654265)。你不應該使用內部的XWork注入機制,它充滿了新的東西來實現相同的目標,更好。 – 2014-09-22 14:15:11

+0

但@Inject在所有其他類中工作 – 2014-09-22 14:18:26

+0

另外,如果您在任何地方使用Scriptlets,它們都可以工作。此外,如果您使用Visual Basic 6,它的工作。但你不應該。 – 2014-09-22 14:23:34

回答

1

official CDI Plugin documentation

使用權@Inject

的Struts 2,它的核心XWork的使用它自己的內部 依賴組件注射容器。有趣的是,你可以將其命名爲 JSR-330的奶奶,因爲它是早期預發佈版本的谷歌 Guice曾經由Crazybob Lee開發 - 同樣的Bob Lee與SpringSource的Rod Johnson一起開發 ,領導JSR-330規範。

也就是說,您會發現@Inject註釋均爲 com.opensymphony.xwork2.inject.Injectjavax.inject.Inject不要 混淆那兩個 - javax.inject.Inject是你想用的 你的Struts 2 CDI插件和CDI集成在一般!雖然你也可以使用Struts的內部註釋,但是對undefined來說可能是 很奇怪 - 所以請檢查你的導入!

然後,而不是com.opensymphony.xwork2.inject.Inject
使用正確的:javax.inject.Inject

+0

如果我使用javax.inject.Inject,是否需要在任何地方做任何其他配置 – 2014-09-22 14:48:15

+0

如何將其設置爲ObjectFactory – 2014-09-22 15:50:44

+0

只需安裝插件 – 2014-09-22 16:04:52

1

對於這個特殊的問題,你應該提供bean配置爲

<bean class="net.viralpatel.struts2.action.UserModulesServices" name="userModulesServices" /> 

的注入將正常工作,但它是供內部使用,

在內部,框架使用自己的依賴注入與Google Guice非常相似的容器。

您應該考慮其他DI框架的機會。請參閱Dependency Injection

+0

但是我在我的struts.xml中有這個bean配置 – 2014-09-22 15:52:42

+0

你有其他類的配置。這個bean配置你應該追加到你的配置中。 – 2014-09-22 16:08:29