Spring for .NET Community Forums    

Go Back   Spring for .NET Community Forums > General > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2008, 10:17 PM
rohead rohead is offline
Junior Member
New User
 
Join Date: Jul 2008
Posts: 3
Default Autowiring arrays

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();
		}
	}
}
Can anyone please help me to understand why Service3 isn't constructor-injected with an array containing instances of Service1 and Service2?

Thanks!

Sean Rohead
Reply With Quote
  #2  
Old 07-26-2008, 11:53 AM
.ben .ben is offline
Senior Member
Spring User
 
Join Date: Oct 2005
Location: Belgium
Posts: 214
Default

This also happens if you use a property instead of a constructor-arg and use byType wiring.
Reply With Quote
  #3  
Old 07-28-2008, 07:07 PM
rohead rohead is offline
Junior Member
New User
 
Join Date: Jul 2008
Posts: 3
Default

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?
Reply With Quote
  #4  
Old 07-28-2008, 10:18 PM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,239
Default

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
Reply With Quote
  #5  
Old 07-29-2008, 12:05 AM
rohead rohead is offline
Junior Member
New User
 
Join Date: Jul 2008
Posts: 3
Default

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."
Reply With Quote
  #6  
Old 07-29-2008, 03:27 PM
Mark Pollack Mark Pollack is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: New York, NY
Posts: 1,556
Default

Hi,
I've verified that this is a bug and will make a fix for the 1.2 M1 release. I'll post back here when it is fixed so you can pick it up in a nightly build.
Cheers,
Mark
Reply With Quote
  #7  
Old 08-01-2008, 06:16 PM
Mark Pollack Mark Pollack is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: New York, NY
Posts: 1,556
Default

Hi,
This has been fixed now, you can pick up a nightly download to try it out.
Mark
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:05 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.