From 2c68fdea43b7034a0d15d58ccac2f6fb188468c2 Mon Sep 17 00:00:00 2001 From: Thomas Trageser Date: Thu, 27 Sep 2012 22:56:17 +0100 Subject: [PATCH] SPRNET-1523 Possibility to define a default init and destroy method in XML configuration document --- .../Factory/Xml/DocumentDefaultsDefinition.cs | 22 ++++++++ .../Factory/Xml/ObjectDefinitionConstants.cs | 9 ++++ .../Xml/ObjectDefinitionParserHelper.cs | 28 ++++++++++ .../Factory/Xml/ObjectsNamespaceParser.cs | 22 ++++++-- .../Factory/Xml/spring-objects-2.0.xsd | 20 +++++++ .../Factory/Xml/default-destroy-methods.xml | 17 ++++++ .../Factory/Xml/default-initializers.xml | 23 ++++++++ .../Factory/Xml/XmlObjectFactoryTests.cs | 54 +++++++++++++++++++ .../Spring.Core.Tests.2010.csproj | 2 + 9 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-destroy-methods.xml create mode 100644 test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-initializers.xml diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/DocumentDefaultsDefinition.cs b/src/Spring/Spring.Core/Objects/Factory/Xml/DocumentDefaultsDefinition.cs index eccaa27d..e277f005 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/DocumentDefaultsDefinition.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Xml/DocumentDefaultsDefinition.cs @@ -33,6 +33,8 @@ namespace Spring.Objects.Factory.Xml private string dependencyCheck; private string lazyInit; private string merge; + private string initMethod; + private string destroyMethod; /// /// 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; } } + + /// + /// Get or sets the init method for the document that's currently parsed. + /// + /// The init method + public string InitMethod + { + get { return initMethod; } + set { initMethod = value; } + } + + /// + /// Gets or sets the destroy method for the document that's currently parsed. + /// + /// The destroy methood + public string DestroyMethod + { + get { return destroyMethod; } + set { destroyMethod = value; } + } } } diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionConstants.cs b/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionConstants.cs index 51c9835e..fefc3d08 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionConstants.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionConstants.cs @@ -95,6 +95,15 @@ namespace Spring.Objects.Factory.Xml /// public const string DefaultMergeAttribute = "default-merge"; + /// + /// Specifies the default init method. + /// + public const string DefaultInitMethodAttribute = "default-init-method"; + + /// + /// Specifies the default destroy method. + /// + public const string DefaultDestroyMethodAttribute = "default-destroy-method"; /// /// Defines a single named object. diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionParserHelper.cs b/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionParserHelper.cs index d4a64c38..98849570 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionParserHelper.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectDefinitionParserHelper.cs @@ -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; } diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectsNamespaceParser.cs b/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectsNamespaceParser.cs index 2188fdf1..1ab1f877 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectsNamespaceParser.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Xml/ObjectsNamespaceParser.cs @@ -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)) { diff --git a/src/Spring/Spring.Core/Objects/Factory/Xml/spring-objects-2.0.xsd b/src/Spring/Spring.Core/Objects/Factory/Xml/spring-objects-2.0.xsd index c5febc79..9896a32b 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Xml/spring-objects-2.0.xsd +++ b/src/Spring/Spring.Core/Objects/Factory/Xml/spring-objects-2.0.xsd @@ -589,6 +589,26 @@ + + + + + + + + + + + + + + diff --git a/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-destroy-methods.xml b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-destroy-methods.xml new file mode 100644 index 00000000..39a8407b --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-destroy-methods.xml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-initializers.xml b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-initializers.xml new file mode 100644 index 00000000..69be468c --- /dev/null +++ b/test/Spring/Spring.Core.Tests/Data/Spring/Objects/Factory/Xml/default-initializers.xml @@ -0,0 +1,23 @@ + + + + + + + 7 + + + + + + 7 + + + + diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs index a4b772bd..55a4efa0 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/Xml/XmlObjectFactoryTests.cs @@ -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); + } + /// /// Test that if a custom initializer throws an exception, it's handled correctly. /// @@ -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; diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj index 521fedd1..1761db63 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj @@ -839,6 +839,8 @@ + +