繁体   English   中英

@Inject 在 Struts 2 中的 Jersey REST 网络服务中不起作用

[英]@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM