這是一個錯誤。
經過對彈簧的過度調試。網源代碼,我們發現:
- 支持會話範圍內的對象由
spring.web
DLL
- 給出的
spring.web.mvc
DLL不依賴於spring.web
- 這意味着,這是不可能實例化一個
MvcApplicationContext
可解決會話作用域對象
以下解決方案顯示使用spring.net在MVC3中完全啓用會話作用域對象的自定義MvcApplicationContext
。
標準應用程序上下文無法解析web範圍的原因是它使用類RootObjectDefinition
whis不知道scope
屬性(在config xml中)。 WebApplicationContext
改爲實例化RootWebObjectDefinition
類型,它知道範圍。
WebObjectsFactory
重寫方法CreateRootObjectDefinition
,該方法返回RootWebObjectDefinition
的實例。這是我們想要從我們的應用程序上下文返回的。這是通過覆蓋方法CreateObjectsFactory
來完成的。
接下來的事情,我們必須覆蓋的方法是CreateXmlObjectDefinitionReader
。當spring從配置中讀取元數據時,如果我們不帶特定的讀者,它將不會分析附加屬性,如scope
。因此,我們將在我們的應用程序上下文中使用WebObjectDefinitionReader
。
對於配置會話作用域對象,可以省略singleton
屬性或將其明確設置爲true
。否則值爲false
會話範圍將被禁用。
實施例:
<objects xmlns="http://www.springframework.net">
<object id="shoppingCart" type="ShoppingCart, ..." singleton="true" scope="session" />
</objects>
步驟分步溶液:
- 創建
MvcWebApplicationContext
從MvcApplicationContext
繼承。您將需要重寫上述兩個方法並創建默認構造函數。
- 創建從
MvcContextHandler
繼承的MvcWebContextHandler
。這將觸發我們的自定義應用程序上下文將被使用。
- 在您的
web.config
中使用自定義上下文處理程序。
- 對於IIS6支持或Visual Studio內置網絡服務器:將
WebSupportModule
添加到system.web
部分。
- 對於IIS7支持:將
WebSupportModule
添加到部分。
web。配置:
<configSections>
<sectionGroup name="spring">
<section name="context" type="YourNamspace.MvcWebContextHandler, YourAssembly"/>
....
</sectionGroup>
....
</configSections>
<!-- IIS6 -->
<system.web>
<httpModules>
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
</httpModules>
</system.web>
<!-- IIS7 -->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true" >
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
</modules>
</system.webServer>
自定義應用程序上下文類:
public class MvcWebApplicationContext: MvcApplicationContext {
public MvcWebApplicationContext(string name, bool caseSensitive, params string[] configurationLocations)
: this(new MvcApplicationContextArgs(name, null, configurationLocations, null, caseSensitive))
{ }
public MvcWebApplicationContext(string name, bool caseSensitive, IApplicationContext parentContext, params string[] configurationLocations)
: this(new MvcApplicationContextArgs(name, parentContext, configurationLocations, null, caseSensitive))
{ }
public MvcWebApplicationContext(MvcApplicationContextArgs args)
: base(args)
{ }
public MvcWebApplicationContext(string name, bool caseSensitive, string[] configurationLocations, IResource[] configurationResources)
: this(new MvcApplicationContextArgs(name, null, configurationLocations, configurationResources, caseSensitive))
{ }
public MvcWebApplicationContext(params string[] configurationLocations)
: this(new MvcApplicationContextArgs(string.Empty, null, configurationLocations, null, false))
{ }
protected override XmlObjectDefinitionReader CreateXmlObjectDefinitionReader(DefaultListableObjectFactory objectFactory)
{
return new WebObjectDefinitionReader(GetContextPathWithTrailingSlash(), objectFactory, new XmlUrlResolver());
}
protected override DefaultListableObjectFactory CreateObjectFactory()
{
return new WebObjectFactory(GetContextPathWithTrailingSlash(), IsCaseSensitive);
}
private string GetContextPathWithTrailingSlash()
{
string contextPath = this.Name;
if (contextPath == DefaultRootContextName)
{
contextPath = "/";
}
else
{
contextPath = contextPath + "/";
}
return contextPath;
}
}
自定義上下文句柄類:
public class MvcWebContextHandler : MvcContextHandler {
protected override Type DefaultApplicationContextType
{
get { return typeof(MvcWebApplicationContext); }
}
}
我們增加了這個bug到Spring.NET的問題跟蹤: https://jira.springsource.org/browse/SPRNET-1450
有你設法解決這個問題?根據[webscopes上的文檔](http://www.springframework.net/doc-latest/reference/html/web-mvc.html#web-mvc-objectscope)在asp.net mvc中使用spring.net,它應該一定是''。是否可以使用' ''來禁用會話狀態? –
Marijn
2011-05-24 12:28:09
好吧,我已閱讀文檔,我完全同意你的看法。儘管如此,實例化對象總是處於請求範圍內,只要我省略「singleton」屬性即可。如果我不明確地將'singleton'設置爲'true',無論我的'scope'屬性如何表示,我甚至無法獲得應用程序作用域對象。這就是讓我如此好奇的原因。但是我還沒有解決它。我將創建一個簡單的MVC2 Hello World應用程序來檢查它是否像文檔所說的那樣工作。我也會檢查我的sessionState模式。當然我會在這裏發佈任何進展。謝謝! – Jan 2011-05-24 17:06:31
我將Spring.NET應用於ASP.NET的默認MVC2應用程序,並重現了相同的行爲:'scope'屬性被忽略,'singleton'屬性創建應用程序範圍對象或請求範圍對象。所以這個問題一定是我。將進一步調查。 – Jan 2011-05-26 13:53:40