Retrieve newly created attribute from underlying request (marking it for update)

Issue: SPR-15300
This commit is contained in:
Juergen Hoeller 2017-03-07 15:41:23 +01:00
parent fc2e635c05
commit f30c498162
2 changed files with 54 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,6 +43,14 @@ public abstract class AbstractRequestAttributesScope implements Scope {
if (scopedObject == null) {
scopedObject = objectFactory.getObject();
attributes.setAttribute(name, scopedObject, getScope());
// Retrieve object again, registering it for implicit session attribute updates.
// As a bonus, we also allow for potential decoration at the getAttribute level.
Object retrievedObject = attributes.getAttribute(name, getScope());
if (retrievedObject != null) {
// Only proceed with retrieved object if still present (the expected case).
// If it disappeared concurrently, we return our locally created instance.
scopedObject = retrievedObject;
}
}
return scopedObject;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.web.context.request;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Before;
@ -48,7 +49,7 @@ public class SessionScopeTests {
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
this.beanFactory.registerScope("session", new SessionScope());
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
reader.loadBeanDefinitions(new ClassPathResource("sessionScopeTests.xml", getClass()));
@ -59,9 +60,17 @@ public class SessionScopeTests {
RequestContextHolder.setRequestAttributes(null);
}
@Test
public void getFromScope() throws Exception {
MockHttpSession session = new MockHttpSession();
AtomicInteger count = new AtomicInteger();
MockHttpSession session = new MockHttpSession() {
@Override
public void setAttribute(String name, Object value) {
super.setAttribute(name, value);
count.incrementAndGet();
}
};
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
@ -70,8 +79,41 @@ public class SessionScopeTests {
String name = "sessionScopedObject";
assertNull(session.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertEquals(1, count.intValue());
assertEquals(session.getAttribute(name), bean);
assertSame(bean, this.beanFactory.getBean(name));
assertEquals(1, count.intValue());
// should re-propagate updated attribute
requestAttributes.requestCompleted();
assertEquals(session.getAttribute(name), bean);
assertEquals(2, count.intValue());
}
@Test
public void getFromScopeWithSingleAccess() throws Exception {
AtomicInteger count = new AtomicInteger();
MockHttpSession session = new MockHttpSession() {
@Override
public void setAttribute(String name, Object value) {
super.setAttribute(name, value);
count.incrementAndGet();
}
};
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(session);
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
String name = "sessionScopedObject";
assertNull(session.getAttribute(name));
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertEquals(1, count.intValue());
// should re-propagate updated attribute
requestAttributes.requestCompleted();
assertEquals(session.getAttribute(name), bean);
assertEquals(2, count.intValue());
}
@Test