2014-09-22 53 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