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



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

的例子:Mozilla

: 首先,我的公司是空的。当我在一个屏幕中选择一个comp时,新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>

我只复制了对我来说至关重要的代码。首先,我想感谢所有花时间帮助我的人。

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

我想你在MVC中稍微混淆了M和C的使用。一般来说,控制器应该是单例的,因此不能包含用户数据。控制器中的userDetails和comp属于模型,在大多数Spring MVC实现中,它们将存储在会话中。你的aRepService看起来像一个由所有用户共享的服务,所以它绝对属于控制器。

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

最新更新