public void RegisterExistingSingletonWithAutowire() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("name", "Tony"); pvs.Add("age", "48"); RootObjectDefinition rod = new RootObjectDefinition(typeof(DependenciesObject), pvs, true); rod.DependencyCheck = DependencyCheckingMode.Objects; rod.AutowireMode = AutoWiringMode.ByType; lof.RegisterObjectDefinition("test", rod); object singletonObject = new TestObject(); lof.RegisterSingleton("singletonObject", singletonObject); Assert.IsTrue(lof.ContainsObject("singletonObject")); Assert.IsTrue(lof.IsSingleton("singletonObject")); Assert.AreEqual(0, lof.GetAliases("singletonObject").Count); DependenciesObject test = (DependenciesObject)lof.GetObject("test"); Assert.AreEqual(singletonObject, lof.GetObject("singletonObject")); Assert.AreEqual(singletonObject, test.Spouse); }
public void RegisterExistingSingletonWithReference() { DefaultListableObjectFactory lof = new DefaultListableObjectFactory(); RootObjectDefinition def = new RootObjectDefinition(); def.ObjectType = typeof(TestObject); def.PropertyValues.Add("Name", "Rick"); def.PropertyValues.Add("Age", 30); def.PropertyValues.Add("Spouse", new RuntimeObjectReference("singletonObject")); lof.RegisterObjectDefinition("test", def); object singletonObject = new TestObject(); lof.RegisterSingleton("singletonObject", singletonObject); Assert.IsTrue(lof.IsSingleton("singletonObject")); TestObject test = (TestObject)lof.GetObject("test"); Assert.AreEqual(singletonObject, lof.GetObject("singletonObject")); Assert.AreEqual(singletonObject, test.Spouse); IDictionary<string, object> objectsOfType = lof.GetObjectsOfType(typeof(TestObject), false, true); Assert.AreEqual(2, objectsOfType.Count); Assert.IsTrue(objectsOfType.Values.Contains(test)); Assert.IsTrue(objectsOfType.Values.Contains(singletonObject)); }