PDA

View Full Version : Getting a Stackoverflow error


k6669999
02-22-2007, 03:24 PM
Hello,
I am new to spring and I am trying to run the example movie finder -- with a few changes that should not change the behavior. I have a program.cs that runs the console program. This program creates an ApplicationContext, calls the MovieLister and gets moviedirectedby. In the app.config I have set up the SimpleMovieFinder as the class that MovieLister calls.

I compile it in visual studio 2005. It compiles and starts. I creates the injection properly as far as I can tell because when it crashes I can check the value of the set method in MovieLister -- it says Spring.ExampleMovieFinder.SimpleMovieFinder. also in the output before crash it shows the list defined in SimpleMovieFinder.

Here is my program.cs, MovieLister.cs, IMovieFinder.cs, SimpleMovieFinder.cs and App.Config:

/////////////////////////////////////////////////////////////////////////////

Program.cs:

using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context;

namespace Spring.ExampleMovieFinder {

class Program {

static void Main(string[] args) {
Console.WriteLine("[Program] Starting...");
Spring.Context.IApplicationContext ctx =
Spring.Context.Support.ContextRegistry.GetContext( );
Console.WriteLine("[Program] After creating ctx...");
MovieLister lister = (MovieLister)ctx.GetObject("MyMovieLister");
Console.WriteLine("[Program] After creating lister...");
String movie = lister.MoviesDirectedBy("Reservoir Dogs");
Console.WriteLine("\nSearching for movie...\n");

Console.WriteLine(movie);

Console.WriteLine("\nMovieApp Done.\n\n");
}
}
}

/////////////////////////////////////////////////////////////////////////////

MovieLister.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace Spring.ExampleMovieFinder
{
public class MovieLister
{
public MovieLister() { }

public IMovieFinder movieFinder {
set {
movieFinder = value;
}

get {
return movieFinder;
}
}

public String MoviesDirectedBy(String MovieName) {
String list = movieFinder.FindAll();

return list;
}
}
}

/////////////////////////////////////////////////////////////////////////////

IMovieFinder.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace Spring.ExampleMovieFinder
{
public interface IMovieFinder
{
String FindAll();
}
}

/////////////////////////////////////////////////////////////////////////////

SimpleMovieFinder.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace Spring.ExampleMovieFinder
{

class SimpleMovieFinder: IMovieFinder
{

private String list = "Desperado, From Dusk Till Dawn, Reservoir Dogs, Kill Bill 1, Kill Bill 2";

public SimpleMovieFinder()
{
Console.WriteLine("[SimpleMovieFinder] in constructor...");
Console.WriteLine("[SimpleMovieFinder] list: " + list);
}

public String FindAll()
{
Console.WriteLine("[SimpleMovieFinder] Find all...");
return list;
}
}
}

/////////////////////////////////////////////////////////////////////////////

App.config:

<?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">
<object name="MyMovieLister"
type="Spring.ExampleMovieFinder.MovieLister, Spring.ExampleMovieFinder">
<!-- using setter injection... -->
<property name="movieFinder" ref="MyMovieFinder"/>
</object>
<object name="MyMovieFinder"
type="Spring.ExampleMovieFinder.SimpleMovieFinder, Spring.ExampleMovieFinder">
</object>
<object name="AnotherMovieFinder"
type="Spring.ExampleMovieFinder.ColonDelimitedMovieFinde r, Spring.ExampleMovieFinder">
</object>
</objects>
</spring>
</configuration>

Here is the output:

[Program] Starting...
[SimpleMovieFinder] in constructor...
[SimpleMovieFinder] list: Desperado, From Dusk Till Dawn, Reservoir Dogs, Kill Bill 1, Kill Bill 2

Here is where it hangs for a second and comes back with StackOverFlowException.

Bruno Baia
02-22-2007, 05:35 PM
public IMovieFinder movieFinder {
set {
movieFinder = value;
}

get {
return movieFinder;
}
}

It can take some time before to find it just with the code.
You are using the property itself to store its own value :)


// The field behind the property
private IMovieFinder _movieFinder;
// The property
public IMovieFinder MovieFinder {
set {
_movieFinder = value;
}
get {
return _movieFinder;
}
}

You can also use a simple field :

public IMovieFinder MovieFinder;



Bruno

k6669999
02-22-2007, 07:26 PM
Thank you very much Bruno. Than worked. :)

k6669999
02-22-2007, 07:46 PM
I noticed that my little movie finder app loaded both objects listed in the app.config file. Even though i only call one of them.

Is this the default behavior?

Bruno Baia
02-23-2007, 11:04 AM
Hi,

This is the default behavior.
By default, objects are created as singletons and cached.

But you change this by using the lazy-init property :

<object name="MyMovieFinder"
type="Spring.ExampleMovieFinder.SimpleMovieFinder, Spring.ExampleMovieFinder"
lazy-init="true"/>
<object name="AnotherMovieFinder"
type="Spring.ExampleMovieFinder.ColonDelimitedMovieFinde r, Spring.ExampleMovieFinder"
lazy-init="true"/>


Don't forget to install XSD schemas to get intellisense with Visual Studio (http://www.springframework.net/doc-latest/reference/html/vsnet.html).
You'll see options you have.


Bruno

k6669999
02-27-2007, 06:26 PM
Thank you.