Java Spring没有为每个会话初始化一个对象

Java Spring没有为每个会话初始化一个对象,java,xml,spring,jsp,spring-mvc,Java,Xml,Spring,Jsp,Spring Mvc,大家好,我这里有个大问题。控制器中的会话无法正常工作。我的目标是每个会话有一个comp对象,但每次都会得到相同的comp(comp只初始化一次,而不是每个会话) 例如: Mozilla: 首先,我的comp是空的。当我在一个屏幕中选择一个组件时,新组件被初始化,一切正常 铬: 当我进入要选择的屏幕时,我的comp已经初始化(Mozilla comp),因此当我选择我的comp时,Mozilla的comp被覆盖 控制器: 组成部分: package.com.test; @组成部分 @范围(prox

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

例如:

Mozilla: 首先,我的comp是空的。当我在一个屏幕中选择一个组件时,新组件被初始化,一切正常

铬: 当我进入要选择的屏幕时,我的comp已经初始化(Mozilla comp),因此当我选择我的comp时,Mozilla的comp被覆盖

控制器:

组成部分:

package.com.test;
@组成部分
@范围(proxyMode=ScopedProxyMode.TARGET_类,value=“session”)
公共类组件实现可序列化{
私有静态最终长serialVersionUID=587780902400791285L;
私有列表项=新的ArrayList();
私有整数长度;
私有整数高度;
私有整数深度;
公共部分(){
}
公共组件(整数长度、整数高度、整数深度){
这个长度=长度;
高度=高度;
这个。深度=深度;
}
}
root-context.xml


servlet-context.xml



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

您还需要对控制器bean会话的范围进行限定

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


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

如果我这样做,控制器的所有对象都是会话范围的,而不仅仅是comp对象。我只想对comp对象会话范围进行定义。不,不是全部,只是那些使用会话范围定义的对象。基本上我认为这不是你所期望的。会话属性用于在多个请求之间保存数据,直到会话失效,所以我想您的组件根本不应该是Springbean。
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;
}
}
<?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>
<?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>