java - Spring 没有为每个 session 初始化一个对象

标签 java xml spring jsp spring-mvc

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

示例:

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

Chrome: 当我到达想要选择的屏幕时,我的组件已经初始化(Mozilla 组件),因此当我选择我的组件时,来自 mozilla 的组件将被覆盖。

Controller :

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>

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

最佳答案

您还需要使 Controller bean session 具有范围

关于java - Spring 没有为每个 session 初始化一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342074/

相关文章:

java - 如何处理认证回调?

java - JAX-WS @webMethod 手动反序列化

iphone - XCODE NSXML更改元素值

javascript - 将 javascript 变量发送到 spring Controller

java - 由于依赖项和/或插件而导致 pom.xml 文件错误

java - 如何从其他 bean 属性值设置 bean 属性值

java - 如何在 Spring Boot linux 中向类路径提供外部配置资源(数据库属性和 XML 文件)

java - 旋转物体使其变小

日语的 Java 编码

php - 在 PHP 中加载格式错误的 XML