This commit is contained in:
Steve Bohlen
2012-10-11 11:00:01 -04:00
9 changed files with 193 additions and 4 deletions

View File

@@ -33,6 +33,8 @@ namespace Spring.Objects.Factory.Xml
private string dependencyCheck;
private string lazyInit;
private string merge;
private string initMethod;
private string destroyMethod;
/// <summary>
/// Gets or sets the autowire setting for the document that's currently parsed.
@@ -73,5 +75,25 @@ namespace Spring.Objects.Factory.Xml
get { return merge; }
set { merge = value; }
}
/// <summary>
/// Get or sets the init method for the document that's currently parsed.
/// </summary>
/// <value>The init method</value>
public string InitMethod
{
get { return initMethod; }
set { initMethod = value; }
}
/// <summary>
/// Gets or sets the destroy method for the document that's currently parsed.
/// </summary>
/// <value>The destroy methood</value>
public string DestroyMethod
{
get { return destroyMethod; }
set { destroyMethod = value; }
}
}
}

View File

@@ -95,6 +95,15 @@ namespace Spring.Objects.Factory.Xml
/// </summary>
public const string DefaultMergeAttribute = "default-merge";
/// <summary>
/// Specifies the default init method.
/// </summary>
public const string DefaultInitMethodAttribute = "default-init-method";
/// <summary>
/// Specifies the default destroy method.
/// </summary>
public const string DefaultDestroyMethodAttribute = "default-destroy-method";
/// <summary>
/// Defines a single named object.

View File

@@ -175,6 +175,34 @@ namespace Spring.Objects.Factory.Xml
#endregion
ddd.InitMethod = GetAttributeValue(root, ObjectDefinitionConstants.DefaultInitMethodAttribute);
#region Instrumentation
if (log.IsDebugEnabled)
{
log.Debug(
string.Format(
"Default init method '{0}'.",
ddd.InitMethod));
}
#endregion
ddd.DestroyMethod = GetAttributeValue(root, ObjectDefinitionConstants.DefaultDestroyMethodAttribute);
#region Instrumentation
if (log.IsDebugEnabled)
{
log.Debug(
string.Format(
"Default destroy method '{0}'.",
ddd.DestroyMethod));
}
#endregion
defaults = ddd;
}

View File

@@ -488,14 +488,28 @@ namespace Spring.Objects.Factory.Xml
}
od.IsPrimary = IsTrueStringValue(primary);
string initMethodName = GetAttributeValue(element, ObjectDefinitionConstants.InitMethodAttribute);
if (StringUtils.HasText(initMethodName))
if (initMethodName != null)
{
od.InitMethodName = initMethodName;
if (StringUtils.HasText(initMethodName))
od.InitMethodName = initMethodName;
}
else
{
if (StringUtils.HasText(childParserContext.ParserHelper.Defaults.InitMethod))
od.InitMethodName = childParserContext.ParserHelper.Defaults.InitMethod;
}
string destroyMethodName = GetAttributeValue(element, ObjectDefinitionConstants.DestroyMethodAttribute);
if (StringUtils.HasText(destroyMethodName))
if (destroyMethodName != null)
{
od.DestroyMethodName = destroyMethodName;
if (StringUtils.HasText(destroyMethodName))
{
od.DestroyMethodName = destroyMethodName;
}
}
else
{
if (StringUtils.HasText(childParserContext.ParserHelper.Defaults.DestroyMethod))
od.DestroyMethodName = childParserContext.ParserHelper.Defaults.DestroyMethod;
}
if (element.HasAttribute(ObjectDefinitionConstants.SingletonAttribute))
{

View File

@@ -589,6 +589,26 @@
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="default-init-method" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The default 'init-method' value; see the documentation for the
'init-method' attribute of the 'object' element.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="default-destroy-method" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The default 'destroy-method' value; see the documentation for the
'destroy-method' attribute of the 'object' element.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>

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

@@ -840,6 +840,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" />