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:
Juergen Hoeller
2009-05-07 22:29:55 +00:00
parent 4ccb352aac
commit 266a65982d
26 changed files with 582 additions and 167 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* 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.
@@ -16,6 +16,8 @@
package org.springframework.aop.scope;
import java.io.Serializable;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.util.Assert;
@@ -32,7 +34,7 @@ import org.springframework.util.Assert;
* @see org.springframework.beans.factory.BeanFactory#getBean
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#destroyScopedBean
*/
public class DefaultScopedObject implements ScopedObject {
public class DefaultScopedObject implements ScopedObject, Serializable {
private final ConfigurableBeanFactory beanFactory;
@@ -43,8 +45,6 @@ public class DefaultScopedObject implements ScopedObject {
* Creates a new instance of the {@link DefaultScopedObject} class.
* @param beanFactory the {@link ConfigurableBeanFactory} that holds the scoped target object
* @param targetBeanName the name of the target bean
* @throws IllegalArgumentException if either of the parameters is <code>null</code>; or
* if the <code>targetBeanName</code> consists wholly of whitespace
*/
public DefaultScopedObject(ConfigurableBeanFactory beanFactory, String targetBeanName) {
Assert.notNull(beanFactory, "BeanFactory must not be null");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -157,29 +157,6 @@ public abstract class AbstractBeanFactoryBasedTargetSource
this.beanFactory = other.beanFactory;
}
/**
* Replaces this object with a SingletonTargetSource on serialization.
* Protected as otherwise it won't be invoked for subclasses.
* (The <code>writeReplace()</code> method must be visible to the class
* being serialized.)
* <p>With this implementation of this method, there is no need to mark
* non-serializable fields in this class or subclasses as transient.
*/
protected Object writeReplace() throws ObjectStreamException {
if (logger.isDebugEnabled()) {
logger.debug("Disconnecting TargetSource [" + this + "]");
}
try {
// Create disconnected SingletonTargetSource.
return new SingletonTargetSource(getTarget());
}
catch (Exception ex) {
logger.error("Cannot get target for disconnecting TargetSource [" + this + "]", ex);
throw new NotSerializableException(
"Cannot get target for disconnecting TargetSource [" + this + "]: " + ex);
}
}
@Override
public boolean equals(Object other) {

View File

@@ -16,6 +16,9 @@
package org.springframework.aop.target;
import java.io.NotSerializableException;
import java.io.ObjectStreamException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory;
@@ -23,8 +26,8 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
/**
* Base class for dynamic TargetSources that can create new prototype bean
* instances to support a pooling or new-instance-per-invocation strategy.
* Base class for dynamic {@link TargetSource} implementations that create new prototype
* bean instances to support a pooling or new-instance-per-invocation strategy.
*
* <p>Such TargetSources must run in a {@link BeanFactory}, as it needs to
* call the <code>getBean</code> method to create a new prototype instance.
@@ -83,4 +86,32 @@ public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFac
}
}
//---------------------------------------------------------------------
// Serialization support
//---------------------------------------------------------------------
/**
* Replaces this object with a SingletonTargetSource on serialization.
* Protected as otherwise it won't be invoked for subclasses.
* (The <code>writeReplace()</code> method must be visible to the class
* being serialized.)
* <p>With this implementation of this method, there is no need to mark
* non-serializable fields in this class or subclasses as transient.
*/
protected Object writeReplace() throws ObjectStreamException {
if (logger.isDebugEnabled()) {
logger.debug("Disconnecting TargetSource [" + this + "]");
}
try {
// Create disconnected SingletonTargetSource.
return new SingletonTargetSource(getTarget());
}
catch (Exception ex) {
logger.error("Cannot get target for disconnecting TargetSource [" + this + "]", ex);
throw new NotSerializableException(
"Cannot get target for disconnecting TargetSource [" + this + "]: " + ex);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* 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.
@@ -16,9 +16,7 @@
package test.util;
import static org.junit.Assert.*;
import java.awt.Point;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -28,8 +26,8 @@ import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import static org.junit.Assert.*;
import org.junit.Test;
import test.beans.TestBean;
/**
@@ -41,13 +39,13 @@ import test.beans.TestBean;
* @author Chris Beams
*/
public final 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);
@@ -57,7 +55,7 @@ public final class SerializationTestUtils {
return false;
}
}
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
@@ -69,30 +67,30 @@ public final class SerializationTestUtils {
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
Object o2 = ois.readObject();
return o2;
}
@Test(expected=NotSerializableException.class)
public void testWithNonSerializableObject() throws IOException {
TestBean o = new TestBean();
assertFalse(o instanceof Serializable);
assertFalse(isSerializable(o));
testSerialization(o);
}
@Test
public void testWithSerializableObject() throws Exception {
int x = 5;
int y = 10;
Point p = new Point(x, y);
assertTrue(p instanceof Serializable);
testSerialization(p);
assertTrue(isSerializable(p));
Point p2 = (Point) serializeAndDeserialize(p);
assertNotSame(p, p2);
assertEquals(x, (int) p2.getX());