簡體   English   中英

Web應用程序上下文中的Spring bean范圍很小

[英]Spring bean scopes in web application context hierarhy

我在web.xml文件中配置了spring root web context。 我和這個父母也有幾個子語境。 所有子上下文都是手動創建的:

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"ApplicationContext/beans.xml"}, rootApplicationContext);

我想在這個子上下文中管理會話和請求范圍bean。

如何正確創建和配置子上下文,使它們能夠處理Web應用程序范圍?

現在我在嘗試自動裝配會話范圍bean時出現以下錯誤(顯然):

java.lang.IllegalStateException: No Scope registered for scope 'session'

你遇到的問題是

session-scope:將單個bean定義范圍限定為HTTP會話的生命周期。 僅在Web感知Spring ApplicationContext的上下文中有效。

並且您的ClassPathXmlApplicationContext不支持Web

我建議你去GenericWebApplicationContext而不是ClassPathXmlApplicationContext

你可以嘗試這樣的事情:

GenericWebApplicationContext context = new GenericWebApplicationContext(servletContext);
context.setParent(rootApplicationContext);
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
xmlReader.loadBeanDefinitions(new ClassPathResource("ApplicationContext/beans.xml"));
context.refresh();

Spring javadoc是有用的資源:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM