I've got an issue where i'm trying to call an object in spring.net through a ProxyFactoryObject. Now the the object that i'm calling in the factory has one constructor which takes in another object by reference through constructor injection. When i call the proxy from the c# code the correct target object is returned from the proxy but the items inside it that should be set to something through the initital constructor call are empty. Now i'm not sure why this is happening.
MY APP.CONFIG Objects section is below:
<objects xmlns="http://www.springfra开发者_Python百科mework.net">
<object name="MyMovieLister" id="MyMovieLister" type="SpringTestProgram.Classes.MovieLister, SpringTestProgram" singleton="false">
<!-- using cstor injection... -->
<constructor-arg index="0" ref="MyMovieFinder"/>
<constructor-arg index="1" value="2"/>
</object>
<object name="MyMovieFinder" id="MyMovieFinder" type="SpringTestProgram.Classes.MovieFinder, SpringTestProgram" singleton="false"/>
<object id="MyBeforeAdvisor"
type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
<property name="Advice">
<object id="myBeforeAdvice"
type="SpringTestProgram.Classes.LoggingAdvice, SpringTestProgram"/>
</property>
<property name="MappedNames">
<list>
<value>FindAll</value>
</list>
</property>
</object>
<object name="MovieWorkerProxy" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
<property name="Target" ref="MyMovieLister" />
<!--<property name="TargetSource">
<object type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop">
<property name="TargetObjectName" value="MyMovieLister" />
</object>
</property>-->
<!--<property name="ObjectNames">
<list>
<value>MyMovieLister</value>
</list>
</property>-->
<property name="interceptorNames">
<list>
<value>MyBeforeAdvisor</value>
</list>
</property>
</object>
</objects>
And below is the c# code that i have to call this:
IApplicationContext ctx = ContextRegistry.GetContext();
MovieLister myMovieLister = ctx.GetObject("MovieWorkerProxy") as MovieLister;
IList<string> myList = myMovieLister.FindMovies();
Now the actual implemention of the class is really not important but the only class that we are really concerned with here is listed below:
public class MovieLister
{
public MovieLister(IMovieFinder myMovieFinder, int i)
{
number = i;
movieFinder = myMovieFinder;
}
public IList<string> FindMovies()
{
return movieFinder.FindAll();
}
private int number;
private IMovieFinder movieFinder;
}
Now i'm not sure why when i call the class above from my proxy factory do both of the fields that are set by the only constructor set to null ? Can anyone help......
Short answer: Make number and movieFinder a property, add the virtual keyword to the properties and the FindMovies method and use the properties instead of the fields directly.
Longer answer: It has to do with the way the proxy is constructed, take a look at Erich Eichingers's blog on how Spring.NET creates proxies. Part 1, 2 and 3.
And since you are proxying a class, take a look at the Spring.NET documentation, it indicates the problem with non virtual methods.
精彩评论