Question

I am learning Spring.Net. I did a little example to understand the tag autowire but it does not work. Below my classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext context;
            context = new XmlApplicationContext("spring.xml");
            Texte texte = null;
            texte = (Texte)context.GetObject("texte");
            texte.print();
            Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
    class Texte
    {
        private String _t;
        private Description _desc;
        public String T
        {
            get { return _t; }
            set { _t = value; }
        }
        internal Description Desc
        {
            get { return _desc; }
            set { _desc = value; }
        }
        public void print()
        {
            Console.WriteLine("text in object: " + _t);
            Console.WriteLine("text description: " + _desc.D);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
    class Description
    {
        private String _d;
        public String D
        {
            get { return _d; }
            set { _d = value; }
        }
    }
}

and here my xml file:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object id = "texte" type = "ConsoleApplication6.Texte" autowire="byName">
    <property name = "_t" value = "I am the Text"/>
  </object>
  <object id = "_desc" type = "ConsoleApplication6.Description">
    <property name = "_d" value = "I am the description"/>
  </object>
</objects>

The Text object is instanciated but it seems that my object Description is not instanciated.

I read the documentation and I checked all the documentation that I can find: http://springindepth.com/book/in-depth-ioc-autowiring.html http://www.berchtel.com/archive/diplomathesis/html/05.4-spring_.net.html

I also checked all questions in stackoverflow related to autowire but most of them are related to the autowire annotation (I did no try this annotation).

Does any one of you have an idea on where is the problem?

Was it helpful?

Solution

Your first reference is actually a book on the Java version of Spring. Spring.net is a port to .net of the Spring for Java framework, with many similarities, but also some differences. The documentation on spring.net is at www.springframework.net/.

According to the docs on autowiring:

[Spring.net] will look for an object named exactly the same as the property which needs to be autowired. For example, if you have an object definition that is set to autowire by name, and it contains a Master property, Spring.NET will look for an object definition named Master, and use it as the value of the Master property on your object definition.

So I guess you have to change your object definition to

<object id = "Desc" type = "ConsoleApplication6.Description">
  <property name = "_d" value = "I am the description"/>
</object>

And you might have to make Texte.Desc public instead of internal, but I'm not really sure about that one.

Update

Well, this works for me:

using System;
using NUnit.Framework;
using Spring.Context.Support;

namespace XmlConfig.AutoWireByName
{
    [TestFixture]
    public class AutoWireByName
    {
        [Test]
        public void LetsAutoWireByName()
        {
            var ctx = new XmlApplicationContext("AutoWireByName/objects.xml");
            var texte = (Texte)ctx.GetObject("texte");
            texte.Print();
        } 
    }

    class Texte
    {
        public string T { get; set; }
        public Description Desc { get; set; }

        public void Print()
        {
            Console.WriteLine("text in object: " + T);
            Console.WriteLine("text description: " + Desc.D);
        }
    }

    class Description
    {
        public string D { get; set; }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object id = "texte" type = "XmlConfig.AutoWireByName.Texte, XmlConfig" autowire="byName">
    <property name = "T" value = "I am the Text"/>
  </object>
  <object id = "Desc" type = "XmlConfig.AutoWireByName.Description, XmlConfig">
    <property name = "D" value = "I am the description"/>
  </object>
</objects>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top