新泽西州和Spring集成 - 豆注射在运行时为空 [英] Jersey and spring integration - bean Injections are null at runtime

查看:117
本文介绍了新泽西州和Spring集成 - 豆注射在运行时为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想中注入服务正在使用泽西休市类。

不管是什么或如何我尝试注入到这个类似乎显示了在运行时为空。寻找在日志文件中表明,当Web应用程序被初始化setJsonTestService被调用,并且它不是在此时为空。但是,当访问后来用PUT请求这个类,它为null。

我完全莫名其妙。

类看起来是这样的:

  @Named
@Path(JsonTest)
公共类JsonTest {
    @Context
    请求请求;
    @Context
    UriInfo uriInfo;    保护最终记录器记录= Logger.getLogger(的getClass());    私人JsonTestService jsonTestService;    @Autowired
    公共无效setJsonTestSerivce(JsonTestService jsonTestService){
        log.info(设置JsonTestService。);
        如果(jsonTestService == NULL){
            log.info(JsonTestService是在注射空);
        }
        this.jsonTestService = jsonTestService;
    }    @注入
    公共ScrapIntermediate scrapIntermediate; //只是用一个简单的空类是真实的方法    @放
    @Path((编号))
    @Produces(MediaType.APPLICATION_JSON)
    公共无效putJson(@PathParam(ID)字符串ID){
        log.info(+ ID在把一些JSON);
        如果(scrapIntermediate == NULL){
            log.info(scrapIntermediate为空...);
        }
        如果(jsonTestService!= NULL){
            jsonTestService.sendUpdate();
        }其他{
            log.info(jsonTestService为空...);
        }
    }
}

任何想法?

更新:

的web.xml(新泽西州)

 <&servlet的GT;
    < servlet的名称>新泽西REST服务与LT; / servlet的名称>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.ServletContainer</servlet-class>
    &LT;初始化参数&GT;
        &LT;参数-名称&gt;&com.sun.jersey.config.property.packages LT; /参数 - 名称&gt;
        &LT;参数值&GT;&EDAS LT; /参数值&GT;
    &LT; /初始化参数&GT;
    &所述;负载上启动→1&下; /负载上启动&GT;
&LT; / servlet的&GT;
&LT; Servlet映射&GT;
    &LT; servlet的名称&gt;新泽西REST服务与LT; / servlet的名称&gt;
    &LT; URL模式&GT; / REST / *&LT; / URL模式&GT;
&LT; / Servlet映射&GT;


解决方案

尝试

 &LT;的servlet类&GT;
     com.sun.jersey.spi.spring.container.servlet.SpringServlet
&LT; / servlet的类&GT;

您还需要 contextConfigLocation的 &LT;的context-param&GT; ,但我相信你有它。 见这里有关设置的更多细节。

马特b建议春季实例化对象,但泽西不知道什么春天,再次实例化它们本身。当您使用 SpringServlet 应该找到Spring应用程序上下文。

这是说,春天MVC提供了RESTful服务,这是非常相似的JAX-RS的支持。你可以试试它。

I am trying to inject services into a Rest class that is using Jersey.

No matter what or how I try to inject into this class seems to be showing up as null at runtime. Looking in the log files shows that the setJsonTestService is being called when the web app is initialized and that it isn't null at this point. But, when access it later with a PUT request to this class, it is null.

I am completely baffled.

The class looks like this:

@Named
@Path("JsonTest")
public class JsonTest {
    @Context
    Request request;
    @Context
    UriInfo uriInfo;

    protected final Logger log = Logger.getLogger(getClass());

    private JsonTestService jsonTestService;

    @Autowired
    public void setJsonTestSerivce(JsonTestService jsonTestService) {
        log.info("Setting JsonTestService.");
        if (jsonTestService == null) {
            log.info("JsonTestService is null at injection");
        }
        this.jsonTestService = jsonTestService;
    }

    @Inject
    public ScrapIntermediate scrapIntermediate; // just a plain empty class with an is true method

    @PUT
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void putJson(@PathParam("id") String id) {
        log.info("Putting some json at " + id);
        if (scrapIntermediate == null) {
            log.info("scrapIntermediate is null...");
        }
        if (jsonTestService != null) {
            jsonTestService.sendUpdate();
        } else {
            log.info("jsonTestService is null...");
        }
    } 
}

Any ideas?

Update:

web.xml (Jersey)

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>EDAS</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

解决方案

Try

<servlet-class>
     com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>

You will also need the contextConfigLocation <context-param>, but I assume you have it. See here for more details about the setup

As matt b suggested spring instantiates your objects, but Jersey does not know anything about spring and instantiates them itself again. When you use the SpringServlet it should locate the spring application context.

That said, spring-mvc provides support for RESTful services that is very similar to that of JAX-RS. You can try it as well.

这篇关于新泽西州和Spring集成 - 豆注射在运行时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆