【发布时间】:2016-12-02 22:11:20
【问题描述】:
我遇到了 spring mvc 返回“Singleton”对象的问题,从而将所有字段暴露给多个(同时)Web 请求。 我试图将范围更改为“请求”,因此我可以获得控制器/服务和 dao 对象的新实例,但我无法让它按照 HTTP 请求工作。
以下是我的 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>
下面是“Count-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 请求创建一个对象。 谁能告诉我哪里出错了?
【问题讨论】:
-
定义“不工作”。你的代码在做什么?你期望会发生什么?会发生什么?
-
两个单独的 HTTP 请求获取相同的对象,因此两个请求都可以看到这些字段。如果两者都是同时请求,Request2 经常会遇到关闭的连接/结果集,因为 Request 1 在完成工作后关闭了这些连接/结果集
-
我希望为每个 http 请求创建一个单独的对象,因此两个同时请求不会相互冲突。这就是我切换到 @scope("request")... 的意图...但是使用 {@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode =ScopedProxyMode.TARGET_CLASS)} 没有任何区别。
标签: spring-mvc web-applications scope request singleton