I have spring root web context configured in web.xml file. I also have several child contexts with this parent. All child contexts are created manually:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"ApplicationContext/beans.xml"}, rootApplicationContext);
I want to manage session and request scoped beans in this child context.
How to create and configure child contexts correctly to make them able to handle web application scopes?
Now I have following error while trying to autowire session scoped bean (obviously):
java.lang.IllegalStateException: No Scope registered for scope 'session'
The problem you have is that
session-scope : Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
And your ClassPathXmlApplicationContext
is not web-aware.
I suggest you to go to GenericWebApplicationContext
instead of ClassPathXmlApplicationContext
You can try something like this:
GenericWebApplicationContext context = new GenericWebApplicationContext(servletContext);
context.setParent(rootApplicationContext);
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
xmlReader.loadBeanDefinitions(new ClassPathResource("ApplicationContext/beans.xml"));
context.refresh();
Spring javadoc is useful source: