2012-02-14 144 views
0

我有一个类,如下图所示:春季球衣REST框架

@Path("/myrequest") 
@Scope("request") 
@Component 
public class MyRESTCode implements IServicedResource<T> { 
@Inject 
private IMyService serviceImpl; 

@Override 
public void setServiceImpl(IMyService impl) { 
    serviceImpl = impl; 
} 
} 

@Path("/users") 
@POST 
@Consumes ({MediaType.APPLICATION_JSON}) 
@Produces ({MediaType.APPLICATION_JSON}) 
public Response mymethod(Object obj) throws Exception { 
     serviceImpl.callme(obj); 
     return Response.noContent().build(); 
} 

现在,这个呼我方法实现在其它的类(MyOtherClass.java)存在。

任何人都可以告诉我MyOtherClass.java中mymethod调用callme方法时,/用户POST请求被建立吗???

此外,谁调用setServiceImpl方法&它是如何设置的&它什么时候被调用?

谢谢!

回答

0

您必须具有弹性的applicationContext.xml这应该是这个样子

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

     <context:component-scan base-package="test.example"/> 
     <context:annotation-config/> 

</beans> 

在该XML,如果您发现基本包应该被定义为一个包在您的控制器类(在你的情况MyRESTCode.java)应该存在。 Spring将搜索注解为@Component的类,并将它们配置为@Path所提及的路径

当您在URL ..../myrequest/users的正文中使用JSON命令发布请求时,callme方法被调用,然后调用你的服务方法。

@Inject annotation告诉spring应该通过setter方法setServiceImpl将IMyService的依赖注入到serviceImpl变量中。

希望这会有所帮助。