Denetleyicideki nesne çalışma zamanında enjekte edilmiyor.Spring .NET ile MVC denetleyicisine bağımlılığı enjekte etme
Web.config:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
. . .
<!-- Spring Context Configuration -->
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects configSource="App_Config\Spring.config" />
</spring>
<!-- End Spring Context Configuration -->
Spring.config:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<!-- Crest is the WCF service to be exposed to the client, could not use a singleton -->
<object id="TestController" type="Project.Controllers.TestController, Project" singleton="false">
<property name="ObjectA" ref="ObjectImpl"/>
</object>
<object id="ObjectImpl" type="Project.Code.Implementations.ClassA, Project" singleton="false" />
</objects>
TestController:
public class TestController: Controller
{
// this object will be injected by Spring.net at run time
private ClassA ObjectA { get; set; }
Sorun: Çalışma zamanında
ObjectA enjekte ve boş kalır almaz, nedeni kod boyunca null istisnası.
Alternatif: Spring nesnesini el ile başlatabilir ve aşağıdaki kodu kullanarak nesne alabilirim.
var ctx = ContextRegistry.GetContext();
var objectA = ((IObjectFactory)ctx).GetObject("ObjectImpl") as ClassA;
Bulmacanın bazı parçalarını kaçırıyorsunuz. Lütfen [burada] (http://foldingair.blogspot.hu/2010/01/springnet-and-aspmvc-getting-started.html) ve ayrıca [burada] (http://www.springframework.net) bir göz atın. /doc-latest/reference/html/web-mvc.html) ve [burada] (http://nuget.org/packages/Spring.Web.Mvc/1.3.2). – nemesv
@nemesv evet doğru, bulmacanın bir parçasını kaçırıyordum. Teşekkürler! –