2016-12-14 123 views
0

我想问你一个关于用cxf对bean的范围的问题。 根据文档,所有的bean将在单身范围内。不过,我想让他们在范围要求。范围请求cxf jaxrs

在DOC这里http://cxf.apache.org/docs/jaxrs-services-configuration.html

我发现:

当服务类和供应商注册这种方式,默认的生命周期是“单身”。您可以通过设置“jaxrs.scope”参数的值为'prototype'(相当于每个请求)来覆盖它。

我试过的东西,千,我是不能够让他们的工作:

这里是我的网页XML

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
    <display-name>CxfRestService</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/beans.xml</param-value> 
    </context-param> 
    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/resource/log4j.properties</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>CXFServlet</servlet-name> 
     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
     <init-param> 
      <param-name>jaxrs.scope</param-name> 
      <param-value>request</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>CXFServlet</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
</web-app> 

我做了一个简单的测试:

@Path("/") 
@WebService(name = "profile", targetNamespace = "") 
@Named 
public class ProfileCXFService { 

    private String thisIsAValue; 
    /** 
    * Create a profile 
    */ 
    @POST 
    @Path("") 
    @Produces({ MediaType.APPLICATION_JSON }) 
    @Consumes({ MediaType.APPLICATION_JSON }) 
    public Response createProfile(Profile profile){ 

     if(thisIsAValue == null){ 
      thisIsAValue = "toto"; 
     } else{ 
      return Response.serverError().entity("NOOOOOO").build(); 
     } 
     return Response.ok().entity("YESSSSS").build(); 
    } 

第一个电话是YESSS和第二个NOOOOO。

我想要所有我的豆作为要求。我能找到一种常用的方法来配置它吗?

事实上,我没有单身人士的问题,因为我唯一的领域是服务,这也是单身人士和唯一的时刻,我需要使用字段特定的请求,我正在做一个新的MyObject。但是我听说如果流量在服务器上增长,Singleton可能会造成延迟。

你知道我能做什么吗?

非常感谢提前和问候

杰弗里

回答

-1

所以基本上找到了解决办法:

在你的beans.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://cxf.apache.org/jaxrs 
    http://cxf.apache.org/schemas/jaxrs.xsd 
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

    <jaxrs:server id="profile" address="/profile"> 
     <jaxrs:providers> 
      <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" /> 
     </jaxrs:providers> 
     <jaxrs:serviceBeans> 
      <ref bean="ProfileCXFServiceImpl"/> 
     </jaxrs:serviceBeans> 
     <jaxrs:extensionMappings> 
      <entry key="xml" value="application/xml" /> 
      <entry key="json" value="application/json" /> 
     </jaxrs:extensionMappings> 
    </jaxrs:server> 

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> 

    <bean id="ProfileCXFServiceImpl" class="com.project.cxfrestservice.ProfileCXFServiceImpl" scope="prototype"><aop:scoped-proxy /></bean> 

</beans> 

,瞧!!!!