![]() |
|
#1
|
|||
|
|||
|
In section 5.3.5 of the reference manual, it states, "When using either the byType or constructor autowiring mode, it is possible to wire arrays and typed-collections. In such cases all autowire candidates within the container that match the expected type will be provided to satisfy the dependency."
I am trying to get this to work in the following sample program but I get an error message during autowiring: "Spring.Objects.Factory.UnsatisfiedDependencyExcep tion: Error creating object with name 'TestApp.Service3' : Unsatisfied dependency expressed through constructor argument with index 0 of type [TestApp.IService[]] : There are '0' objects of type [TestApp.IService[]] for autowiring constructor. There should have been exactly 1 to be able to autowire the 'services' argument on the constructor of object 'TestApp.Service3'." Code:
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
namespace TestApp
{
public interface IService
{
}
public class Service1 : IService
{
}
public class Service2 : IService
{
}
public class Service3
{
public Service3(IService[] services) { }
}
class Program
{
static void Main(string[] args)
{
var factory = new DefaultListableObjectFactory();
factory.RegisterObjectDefinition(typeof(Service1).FullName, new RootObjectDefinition(typeof(Service1), AutoWiringMode.Constructor));
factory.RegisterObjectDefinition(typeof(Service2).FullName, new RootObjectDefinition(typeof(Service2), AutoWiringMode.Constructor));
factory.RegisterObjectDefinition(typeof(Service3).FullName, new RootObjectDefinition(typeof(Service3), AutoWiringMode.Constructor));
factory.PreInstantiateSingletons();
}
}
}
Thanks! Sean Rohead |
|
#2
|
|||
|
|||
|
This also happens if you use a property instead of a constructor-arg and use byType wiring.
|
|
#3
|
|||
|
|||
|
I can't get array autowiring to work using a property and byType either. When I run the following program, the Services property is still set to null and is not injected with the expected array.
Code:
using System;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
namespace TestApp
{
public interface IService
{
}
public class Service1 : IService
{
}
public class Service2 : IService
{
}
public class Service3
{
public IService[] Services
{
get; set;
}
}
class Program
{
static void Main(string[] args)
{
var factory = new DefaultListableObjectFactory();
factory.RegisterObjectDefinition(typeof(Service1).FullName, new RootObjectDefinition(typeof(Service1), AutoWiringMode.ByType));
factory.RegisterObjectDefinition(typeof(Service2).FullName, new RootObjectDefinition(typeof(Service2), AutoWiringMode.ByType));
factory.RegisterObjectDefinition(typeof(Service3).FullName, new RootObjectDefinition(typeof(Service3), AutoWiringMode.ByType));
factory.PreInstantiateSingletons();
Service3 service3 = (Service3) factory.GetObject(typeof (Service3).FullName);
Console.WriteLine(service3.Services == null ? 0 : service3.Services.Length);
}
}
}
If I call the overloaded version of RegisterObjectDefinition() and pass 'true' for the dependecyCheck parameter, I get an exception when I call PerInstantiateSingletons(): "Error creating object with name 'TestApp.Service3' : Unsatisfied dependency expressed through object property 'Services':" Does anyone know what I'm doing wrong here? |
|
#4
|
|||
|
|||
|
Hi,
This doesn't work because Spring.NET doesn't find an object of type IService[]. If you register an object of type IService[], it will work. HTH, Bruno
__________________
My english is as poor as my taylor is rich
|
|
#5
|
|||
|
|||
|
It should work based on section 5.3.5 of the reference manual: "When using either the byType or constructor autowiring mode, it is possible to wire arrays and typed-collections. In such cases all autowire candidates within the container that match the expected type will be provided to satisfy the dependency."
|
|
#7
|
|||
|
|||
|
Hi,
This has been fixed now, you can pick up a nightly download to try it out. Mark |
![]() |
| Thread Tools | |
| Display Modes | |
|
|