SPRNET-1523 Possibility to define a default init and destroy method in XML configuration document

This commit is contained in:
Thomas Trageser
2012-09-27 22:56:17 +01:00
parent eb59b8f26e
commit 2c68fdea43
9 changed files with 193 additions and 4 deletions

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"
default-destroy-method="CustomDestroy">
<object id="destroy-method1"
type="Spring.Objects.Factory.Xml.XmlObjectFactoryTests+DefaultDestroyer, Spring.Core.Tests"
lazy-init="true" />
<object id="destroy-method2"
type="Spring.Objects.Factory.Xml.XmlObjectFactoryTests+DefaultDestroyer, Spring.Core.Tests"
lazy-init="true"
destroy-method="" />
</objects>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"
default-init-method="Init">
<object id="init-method1"
type="Spring.Objects.Factory.Xml.XmlObjectFactoryTests+DoubleInitializer, Spring.Core.Tests">
<property name="num">
<value>7</value>
</property>
</object>
<object id="init-method2"
type="Spring.Objects.Factory.Xml.XmlObjectFactoryTests+DoubleInitializer, Spring.Core.Tests"
init-method="">
<property name="num">
<value>7</value>
</property>
</object>
</objects>

View File

@@ -884,6 +884,26 @@ namespace Spring.Objects.Factory.Xml
Assert.AreEqual(14, in_Renamed.Num);
}
[Test]
public void DefaultInitMethodIsInvoked()
{
IResource resource = new ReadOnlyXmlTestResource("default-initializers.xml", GetType());
XmlObjectFactory xof = new XmlObjectFactory(resource);
DoubleInitializer in_Renamed = (DoubleInitializer)xof.GetObject("init-method1");
// Initializer should have doubled value
Assert.AreEqual(14, in_Renamed.Num);
}
[Test]
public void DefaultInitMethodDisabled()
{
IResource resource = new ReadOnlyXmlTestResource("default-initializers.xml", GetType());
XmlObjectFactory xof = new XmlObjectFactory(resource);
DoubleInitializer in_Renamed = (DoubleInitializer)xof.GetObject("init-method2");
// Initializer should have doubled value
Assert.AreEqual(7, in_Renamed.Num);
}
/// <summary>
/// Test that if a custom initializer throws an exception, it's handled correctly.
/// </summary>
@@ -935,6 +955,30 @@ namespace Spring.Objects.Factory.Xml
Assert.IsTrue(iib.destroyed && iib.customDestroyed);
}
[Test]
public void DefaultDestroyMethodInvoked()
{
IResource resource = new ReadOnlyXmlTestResource("default-destroy-methods.xml", GetType());
XmlObjectFactory xof = new XmlObjectFactory(resource);
xof.PreInstantiateSingletons();
DefaultDestroyer dd = (DefaultDestroyer)xof.GetObject("destroy-method1");
Assert.IsTrue(!dd.customDestroyed);
xof.Dispose();
Assert.IsTrue(dd.customDestroyed);
}
[Test]
public void DefaultDestroyMethodDisabled()
{
IResource resource = new ReadOnlyXmlTestResource("default-destroy-methods.xml", GetType());
XmlObjectFactory xof = new XmlObjectFactory(resource);
xof.PreInstantiateSingletons();
DefaultDestroyer dd = (DefaultDestroyer)xof.GetObject("destroy-method2");
Assert.IsTrue(!dd.customDestroyed);
xof.Dispose();
Assert.IsTrue(!dd.customDestroyed);
}
[Test]
public void MultiThreadedLazyInit()
{
@@ -1942,6 +1986,16 @@ namespace Spring.Objects.Factory.Xml
}
}
public class DefaultDestroyer
{
public bool customDestroyed;
public void CustomDestroy()
{
customDestroyed = true;
}
}
public class InitAndIB : IInitializingObject, IDisposable
{
public static bool constructed;

View File

@@ -839,6 +839,8 @@
<Content Include="Data\Spring\Objects\Factory\Xml\collectionMergingGeneric.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\collectionMerging.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\collectionConversionGeneric.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\default-destroy-methods.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\default-initializers.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\ctor-args.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\objectNameGeneration.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\simple-constructor-arg.xml" />