Spring bean scopes in context of hierarhy web application

I have spring root web context set up in web.xml file. I also have multiple 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 scope beans in this child context.

How do I properly create and configure child contexts so that they can handle web application scopes?

Now I have the following error when trying to autowire scope bean (obviously):

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

      

+3


source to share


1 answer


The problem is

session-scope: Displays one bean definition of the HTTP session lifecycle. Valid only in the context of a Web Spring ApplicationContext .

And yours is ClassPathXmlApplicationContext

not supported online.

I suggest you go to GenericWebApplicationContext

insteadClassPathXmlApplicationContext



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 - helpful source:

+4


source







All Articles