Servlet/Portlet ApplicationContexts use a specific id based on servlet/portlet name; DefaultListableBeanFactory references are serializable now when initialized with an id; scoped proxies are serializable now, for web scopes as well as for singleton beans; injected request/session references are serializable proxies for the current request now
This commit is contained in:
@@ -416,6 +416,7 @@ public abstract class FrameworkServlet extends HttpServletBean
|
||||
|
||||
ConfigurableWebApplicationContext wac =
|
||||
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
|
||||
wac.setId(getServletContext().getServletContextName() + "." + getServletName());
|
||||
wac.setParent(parent);
|
||||
wac.setServletContext(getServletContext());
|
||||
wac.setServletConfig(getServletConfig());
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.NotSerializableException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Utilities for testing serializability of objects.
|
||||
* Exposes static methods for use in other test cases.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class SerializationTestUtils {
|
||||
|
||||
public static void testSerialization(Object o) throws IOException {
|
||||
OutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(o);
|
||||
}
|
||||
|
||||
public static boolean isSerializable(Object o) throws IOException {
|
||||
try {
|
||||
testSerialization(o);
|
||||
return true;
|
||||
}
|
||||
catch (NotSerializableException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(o);
|
||||
oos.flush();
|
||||
baos.flush();
|
||||
byte[] bytes = baos.toByteArray();
|
||||
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(is);
|
||||
Object o2 = ois.readObject();
|
||||
return o2;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.annotation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -72,6 +73,7 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
@@ -524,7 +526,7 @@ public class ServletAnnotationControllerTests {
|
||||
GenericWebApplicationContext wac = new GenericWebApplicationContext();
|
||||
wac.setServletContext(servletContext);
|
||||
RootBeanDefinition bd = new RootBeanDefinition(MyParameterDispatchingController.class);
|
||||
bd.setScope(WebApplicationContext.SCOPE_REQUEST);
|
||||
//bd.setScope(WebApplicationContext.SCOPE_REQUEST);
|
||||
wac.registerBeanDefinition("controller", bd);
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
|
||||
wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
|
||||
@@ -541,8 +543,8 @@ public class ServletAnnotationControllerTests {
|
||||
assertEquals("myView", response.getContentAsString());
|
||||
assertSame(servletContext, request.getAttribute("servletContext"));
|
||||
assertSame(servletConfig, request.getAttribute("servletConfig"));
|
||||
assertSame(session, request.getAttribute("session"));
|
||||
assertSame(request, request.getAttribute("request"));
|
||||
assertSame(session.getId(), request.getAttribute("sessionId"));
|
||||
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
|
||||
|
||||
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
|
||||
response = new MockHttpServletResponse();
|
||||
@@ -551,8 +553,8 @@ public class ServletAnnotationControllerTests {
|
||||
assertEquals("myView", response.getContentAsString());
|
||||
assertSame(servletContext, request.getAttribute("servletContext"));
|
||||
assertSame(servletConfig, request.getAttribute("servletConfig"));
|
||||
assertSame(session, request.getAttribute("session"));
|
||||
assertSame(request, request.getAttribute("request"));
|
||||
assertSame(session.getId(), request.getAttribute("sessionId"));
|
||||
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
|
||||
|
||||
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
|
||||
request.addParameter("view", "other");
|
||||
@@ -572,6 +574,11 @@ public class ServletAnnotationControllerTests {
|
||||
response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("mySurpriseView", response.getContentAsString());
|
||||
|
||||
MyParameterDispatchingController deserialized = (MyParameterDispatchingController)
|
||||
SerializationTestUtils.serializeAndDeserialize(servlet.getWebApplicationContext().getBean("controller"));
|
||||
assertNotNull(deserialized.request);
|
||||
assertNotNull(deserialized.session);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1265,13 +1272,13 @@ public class ServletAnnotationControllerTests {
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/myPath.do")
|
||||
private static class MyParameterDispatchingController {
|
||||
private static class MyParameterDispatchingController implements Serializable {
|
||||
|
||||
@Autowired
|
||||
private ServletContext servletContext;
|
||||
private transient ServletContext servletContext;
|
||||
|
||||
@Autowired
|
||||
private ServletConfig servletConfig;
|
||||
private transient ServletConfig servletConfig;
|
||||
|
||||
@Autowired
|
||||
private HttpSession session;
|
||||
@@ -1288,8 +1295,8 @@ public class ServletAnnotationControllerTests {
|
||||
response.getWriter().write("myView");
|
||||
request.setAttribute("servletContext", this.servletContext);
|
||||
request.setAttribute("servletConfig", this.servletConfig);
|
||||
request.setAttribute("session", this.session);
|
||||
request.setAttribute("request", this.request);
|
||||
request.setAttribute("sessionId", this.session.getId());
|
||||
request.setAttribute("requestUri", this.request.getRequestURI());
|
||||
}
|
||||
|
||||
@RequestMapping(params = {"view", "!lang"})
|
||||
|
||||
Reference in New Issue
Block a user