本文整理汇总了C#中Spring.Objects.Factory.Support.DefaultListableObjectFactory.CreateObject方法的典型用法代码示例。如果您正苦于以下问题:C# DefaultListableObjectFactory.CreateObject方法的具体用法?C# DefaultListableObjectFactory.CreateObject怎么用?C# DefaultListableObjectFactory.CreateObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Spring.Objects.Factory.Support.DefaultListableObjectFactory
的用法示例。
在下文中一共展示了DefaultListableObjectFactory.CreateObject方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateObjectDoesAffectContainerManagedSingletons
public void CreateObjectDoesAffectContainerManagedSingletons()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
lof.AddObjectPostProcessor(new SharedStateAwareProcessor(new ISharedStateFactory[] { new ByTypeSharedStateFactory() }, Int32.MaxValue ));
MutablePropertyValues properties = new MutablePropertyValues();
properties.Add(new PropertyValue("Age", 27));
properties.Add(new PropertyValue("Name", "Bruno"));
RootObjectDefinition singleton = new RootObjectDefinition(typeof(TestObject), null, properties);
singleton.IsSingleton = true;
lof.RegisterObjectDefinition("singleton", singleton);
// a call to GetObject() results in a normally configured instance
TestObject to = lof.GetObject("singleton") as TestObject;
Assert.IsNotNull(to);
// props set
Assert.AreEqual(27, to.Age);
Assert.AreEqual("Bruno", to.Name);
// object postprocessors executed!
Assert.AreEqual("singleton", to.ObjectName);
Assert.AreEqual(lof, to.ObjectFactory);
Assert.IsNotNull(to.SharedState);
// altough configured as singleton, calling CreateObject prevents the instance from being cached
// otherwise the container could not guarantee the results of calling GetObject() to other clients.
TestObject to2 = lof.CreateObject("singleton", typeof(TestObject), null) as TestObject;
Assert.IsNotNull(to2);
// no props set
Assert.AreEqual(0, to2.Age);
Assert.AreEqual(null, to2.Name);
// no object postprocessors executed!
Assert.AreEqual(null, to2.ObjectName);
Assert.AreEqual(null, to2.ObjectFactory);
Assert.AreEqual(null, to2.SharedState);
// a call to GetObject() results in a normally configured instance
TestObject to3 = lof.GetObject("singleton") as TestObject;
Assert.IsNotNull(to3);
// props set
Assert.AreEqual(27, to3.Age);
Assert.AreEqual("Bruno", to3.Name);
// object postprocessors executed!
Assert.AreEqual("singleton", to3.ObjectName);
Assert.AreEqual(lof, to3.ObjectFactory);
Assert.IsNotNull(to3.SharedState);
}
}
示例2: CreateObjectDoesNotConfigure
public void CreateObjectDoesNotConfigure()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
MutablePropertyValues properties = new MutablePropertyValues();
properties.Add(new PropertyValue("Age", 27));
properties.Add(new PropertyValue("Name", "Bruno"));
RootObjectDefinition singleton = new RootObjectDefinition(typeof(TestObject), null, properties);
singleton.IsSingleton = true;
lof.RegisterObjectDefinition("singleton", singleton);
TestObject to2 = lof.CreateObject("singleton", typeof(TestObject), null) as TestObject;
Assert.IsNotNull(to2);
// no props set
Assert.AreEqual(0, to2.Age);
Assert.AreEqual(null, to2.Name);
// no object postprocessors executed!
Assert.AreEqual(null, to2.ObjectName);
Assert.AreEqual(null, to2.ObjectFactory);
Assert.AreEqual(null, to2.SharedState);
}
}
示例3: CreateObjectWithCtorArgsOverrided
public void CreateObjectWithCtorArgsOverrided()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
ConstructorArgumentValues arguments = new ConstructorArgumentValues();
arguments.AddNamedArgumentValue("age", 27);
arguments.AddNamedArgumentValue("name", "Bruno");
RootObjectDefinition singleton = new RootObjectDefinition(typeof(TestObject), arguments, new MutablePropertyValues());
singleton.IsSingleton = true;
lof.RegisterObjectDefinition("singleton", singleton);
TestObject to = lof.CreateObject("singleton", typeof(TestObject), new object[] { "Mark", 35 }) as TestObject;
Assert.IsNotNull(to);
Assert.AreEqual(35, to.Age);
Assert.AreEqual("Mark", to.Name);
TestObject to2 = lof.CreateObject("singleton", null, null) as TestObject;
Assert.IsNotNull(to2);
Assert.AreEqual(27, to2.Age);
Assert.AreEqual("Bruno", to2.Name);
}
}
示例4: CreateObjectWithCtorArgsOnSingleton
public void CreateObjectWithCtorArgsOnSingleton()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition singleton
= new RootObjectDefinition(typeof(TestObject));
singleton.IsSingleton = true;
lof.RegisterObjectDefinition("singleton", singleton);
TestObject to = lof.CreateObject("singleton", typeof(TestObject), new object[] { "Mark", 35 }) as TestObject;
Assert.IsNotNull(to);
Assert.AreEqual(35, to.Age);
Assert.AreEqual("Mark", to.Name);
}
}
示例5: CreateObjectWithCtorArgsOnPrototypeOutOfOrderArgs
public void CreateObjectWithCtorArgsOnPrototypeOutOfOrderArgs()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition prototype
= new RootObjectDefinition(typeof(TestObject));
prototype.IsSingleton = false;
lof.RegisterObjectDefinition("prototype", prototype);
try
{
TestObject to2 = lof.CreateObject("prototype", typeof(TestObject), new object[] { 35, "Mark" }) as TestObject;
Assert.IsNotNull(to2);
Assert.AreEqual(35, to2.Age);
Assert.AreEqual("Mark", to2.Name);
}
catch (ObjectCreationException ex)
{
Assert.IsTrue(ex.Message.IndexOf("'Object of type 'System.Int32' cannot be converted to type 'System.String'") >= 0);
}
}
}
示例6: CreateObjectWithCtorArgsAndCtorAutowiring
public void CreateObjectWithCtorArgsAndCtorAutowiring()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition prototype = new RootObjectDefinition(typeof(TestObject));
prototype.IsSingleton = false;
lof.RegisterObjectDefinition("prototype", prototype);
TestObject to = lof.CreateObject("prototype", typeof(TestObject), new object[] { "Bruno", 26, new NestedTestObject("Home") }) as TestObject;
Assert.IsNotNull(to);
Assert.AreEqual(26, to.Age);
Assert.AreEqual("Bruno", to.Name);
Assert.AreEqual("Home", to.Doctor.Company);
}
}