2016-12-02 95 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