Question

I am new to Spring.Net. I have created simple console application and wanted to get object of class.

Code:

namespace ConsoleApplicationApring
{
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext context = new XmlApplicationContext(
                @"E:\VS Projects\ConsoleApplicationApring\ConsoleApplicationApring\XMLFile1.xml");

            Car car = (Car)context.GetObject("MyCar");
        }
    }

    public interface ICar
    {
        void Move();
    }

    public class Car : ICar
    {

        public void Move()
        {
            Console.WriteLine("In the Car");
        }
    }
}

XML File:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
      <object name="MyCar"
      type="ConsoleApplicationApring.Car, ConsoleApplicationApring" >
      </object>
    </objects>
  </spring>
</configuration>

When I run application, I get the exception on line "Car car = (Car)context.GetObject("MyCar");" NoSuchObjectDefiniation found : Can not find Definition of Object[MyCar]

Thanks in advance..

Was it helpful?

Solution

Your xml file is like an application configuration file (app.config), but when using an an XmlApplicationContext like you do, it should be a plain xml file, see for instance this example of a plain xml configuration file.

Basically, your XMLFile1.xml should be something like:

<objects xmlns="http://www.springframework.net">
  <object name="MyCar"
  type="ConsoleApplicationApring.Car, ConsoleApplicationApring" >
  </object>
</objects>

This documented in section 5.2.2 of the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top