2016-12-02 77 views
1

我遇到了spring mvc返回一個「Singleton」對象的問題,從而將所有字段暴露給多個(同時)Web請求。 我試圖將範圍更改爲「請求」,所以我可以獲得控制器/服務和dao對象的新實例,但是我無法根據HTTP請求獲取它。@scope(「請求」)不工作,spring mvc仍然返回一個singleton

下面是我的DAO,控制器,服務的代碼示例和域對象

@Component 
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode =ScopedProxyMode.TARGET_CLASS) 
public class CountDaoImpl implements CountDao { 

private static final long serialVersionUID = 1L; 
private static Connection conn = null; 

} 

@Controller(value="countController") 
@RequestMapping("VIEW") 
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode =ScopedProxyMode.TARGET_CLASS) 
public class CountController { 

@Autowired 
@Qualifier("CountServiceImpl") 
CountService CountService; 
. 
. 
. 
} 

@Service("CountServiceImpl") 
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode  =ScopedProxyMode.TARGET_CLASS) 
public class CountServiceImpl implements CountService { 

@Autowired 
@Qualifier("countDaoImpl") 
private CountDao CountDao; 
} 

@Component 
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode =ScopedProxyMode.TARGET_CLASS) 
public class Count implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 9931247627087666L; 
private Owners owners; 
. 
. 
. 
. 
. 
. 

}

下面是我的web.xml就像在spring docs提到,有 「RequestContextListener」

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>PortletApp</display-name> 
<session-config> 
    <session-timeout>0</session-timeout> 
</session-config> 
<jsp-config> 
    <taglib> 
    <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri> 
    <taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location> 
    </taglib> 
    <taglib> 
    <taglib-uri>http://liferay.com/tld/aui</taglib-uri> 
    <taglib-location>/WEB-INF/tld/aui.tld</taglib-location> 
    </taglib> 
</jsp-config> 
<context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/classes/log4j.properties</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>view-servlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>view-servlet</servlet-name> 
     <url-pattern>/WEB-INF/servlet/view</url-pattern> 
    </servlet-mapping> 
</web-app> 

下面是 「計數-portlet.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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util-3.0.xsd 
     "> 
    <context:annotation-config /> 
    <context:component-scan base-package="com.company.count" /> 
    <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 


    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

我想爲每個上述類的HTTP請求創建一個對象。 有人能告訴我我要去哪裏嗎?

+0

定義「不工作」。你的代碼在做什麼?你期望發生什麼?會發生什麼呢? –

+0

兩個單獨的HTTP請求獲取相同的對象,因此這兩個字段對這兩個請求都可見。如果兩者都是同時發生的請求,Request2經常會遇到關閉的連接/結果集,因爲請求1在完成它的工作後關閉了這些請求 – user1751510

+0

我希望根據http請求創建單獨的對象,因此兩個同時發生的請求不會相互衝突。這是我改變@scope(「request」)的意圖......但使用{@Scope(value = WebApplicationContext.SCOPE_REQUEST,proxyMode = ScopedProxyMode.TARGET_CLASS)}沒有任何區別。 – user1751510

回答

1

我知道這是晚了,但我希望這可以幫助別人。

@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode =ScopedProxyMode.INTERFACES) 

我猜,因爲我所有的類都在實現接口我使用這種代理模式。

+1

它做到了。非常感謝 – masadwin

相關問題