【问题标题】:Spring not initializing one object per sessionSpring没有在每个会话中初始化一个对象
【发布时间】:2014-10-10 02:43:05
【问题描述】:

大家好,我这里有大问题。我的控制器中的会话无法正常工作。我的目标是为每个会话拥有一个 comp 对象,但每次我都会得到相同的 comp(comp 只初始化一次,而不是每个会话)。

例子:

Mozilla: 首先我的补偿是空的。当我在一个屏幕中选择一个合成时,新合成被初始化并且一切正常。

铬: 当我到达要选择我的 comp 的屏幕时,我的 comp 已经初始化(Mozilla comp),所以当我选择我的 comp 时,来自 mozilla 的 comp 被覆盖。

控制器:

package com.test;
@SessionAttributes({"comp", "userDetails"})    
@Controller    
@RequestMapping(value="/arep") 
public class ARepController{
@Autowired AdmUserDetails userDetails;

@Autowired
private ARepService aRepService;

@Autowired
private Component comp;


}

组件:

package com.test;
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "session")
public class Component implements Serializable {

private static final long serialVersionUID = 587780902400791285L;

private List<Component Item> items = new ArrayList<ComponentItem>();

private Integer length;
private Integer height;
private Integer depth;

public Component () {
}

public Component (Integer length, Integer height,Integer depth) {
     this.length = length;
     this.height = height;
     this.depth = depth;
}
}

root-context.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:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" >

<tx:annotation-driven/>
<context:component-scan base-package="com.test" >
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

</beans>

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

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

<context:component-scan base-package="com.test" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

</beans:beans>

我只复制了对我来说至关重要的代码。我要提前感谢任何愿意花时间帮助我的人。

【问题讨论】:

    标签: java xml spring jsp spring-mvc


    【解决方案1】:

    我认为您对 MVC 中 M 和 C 的使用有点混淆了。通常控制器应该是单例的,因此不能包含用户数据。控制器中的 userDetails 和 comp 属于模型,并且在大多数 Spring MVC 实现中它们将存储在会话中。你的 aRepService 看起来像是一个所有用户共享的服务,所以它肯定属于控制器。

    Spring 可以创建你的模型 bean(我相信),但这是一项简单的任务,所以我总是手动完成。这也避免了 Spring bean 创建的一些沉重的开销。

    【讨论】:

      【解决方案2】:

      您还需要使您的控制器 bean 会话范围

      【讨论】:

      • 如果我这样做,Controller 的所有对象都是会话范围的,而不仅仅是 comp 对象。我只想要 comp 对象会话范围。
      • 不,不是全部,只有在会话范围内定义的那些。基本上我认为这不是你所期望的。会话属性用于在多个请求之间保留数据,直到会话无效,所以我猜你的组件根本不应该是一个spring bean。
      猜你喜欢
      • 2016-08-23
      • 2015-10-09
      • 2017-07-20
      • 2013-05-22
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多