简体   繁体   English

如何将 spring beans 注入 jersey 的资源类?

[英]How can I inject spring beans into jersey's resources class?

First I define a jersey resource, it defines a class GetAllProgramDesc, then inject a java beans "IDataDevProgram" into it.首先我定义了一个 jersey 资源,它定义了一个类 GetAllProgramDesc,然后向其中注入了一个 java bean“IDataDevProgram”。

   @Path("/datadev")
   public class GetAllProgramDesc {


private IDataDevProgram dataProgram;

public IDataDevProgram getDataProgram() {
    return dataProgram;
}

public void setDataProgram(IDataDevProgram dataProgram) {
    this.dataProgram = dataProgram;
}


// The Java method will produce content identified by the MIME Media
// type "text/plain"
@GET @Path("/mbpprograms") 
@Produces("application/json")
public String getClichedMessage() {
    // Return some cliched textual content
    List<MbpProgram> list=dataProgram.showMbpProgramList(21294551);
    return JSONObject.toJSONString(list);
}   

} }

Then I wanna to inject a java beans into jersey resource class:然后我想将一个 java bean 注入 jersey 资源类:

<bean id="dataDevProgram" class="com.taobao.gemstone.data.mbpapi.datadev.DataDevProgram">
    <property name="mDBops" ref="dataDevDBOps" />
    <property name="sqlManager" ref="sqlManger" />
    <property name="actionManager" ref="actionManger" />
    <property name="dataManager" ref="dataManageImpl" />
    <property name="constant" ref="constantproperty"/>
</bean>

<bean id="datadevrest"    class="com.taobao.gemstone.data.mbpapi.restresources.GetAllProgramDesc">
    <property name="dataProgram" ref="dataDevProgram" />
</bean>

However, when i send a query to this url, the whole process crashed as follows:但是,当我向这个 url 发送查询时,整个过程崩溃如下:

java.lang.NullPointerException com.taobao.gemstone.data.mbpapi.restresources.GetAllProgramDesc.getClichedMessage(GetAllProgramDesc.java:48) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:601) java.lang.NullPointerException com.taobao.gemstone.data.mbappi.restresources.GetAllProgramDesc.getClicedMessage(GetAllProgramDesc.java:48) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethod:Accessor) 57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:601)

is there any method to deal with this problem?有什么方法可以解决这个问题吗?

If you want to use Spring in Jersey you can use one of the following:如果您想在 Jersey 中使用 Spring,您可以使用以下方法之一:

Inject @Context ServletContext context;注入@Context ServletContext context; into your resource class,进入你的资源类,

then use code such as:然后使用代码,例如:

WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context); IDataDevProgram dataProgram = applicationContext.getBean(IDataDevProgram .class);

Alternatively, you can also use jersey support for IoC container:或者,您也可以对 IoC 容器使用 jersey 支持:

@InjectParam IDataDevProgram dataProgram

You will have to use jersey-spring contrib and apply SpringServlet in your web.xml , there are numerous short tutorials which explain how to do that您将不得不使用jersey-spring contrib 并在您的web.xml应用SpringServlet ,有许多简短的教程解释了如何做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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