0

I am trying to create a Spring bean in session scope:

<bean id="myClass" class="com.test.Myclass scope="session" />

When I try to get the class from ApplicationContext in a servlet filter, like this:

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
Myclass obj = (Myclass)applicationContext.getBean("myClass");

I get the following error: java.lang.IllegalStateException: No Scope registered for scope name 'session'

I don't have any issues if I define the scope as prototype! How can I retrieve the session-scope spring bean from applicationContext ?

alexbt
  • 16,415
  • 6
  • 78
  • 87

1 Answers1

1

I already answered that question before, look here how to define session scope bean:

Spring autowireing a session scope bean in AuthenticationSuccessHandler is not working

Community
  • 1
  • 1
Moshe Arad
  • 3,587
  • 4
  • 18
  • 33
  • Was it helpful? – Moshe Arad Nov 06 '16 at 21:29
  • Everything in place except adding the bean in dispatcher-servlet.xml – Anbu Chinna Nov 06 '16 at 21:29
  • my bean definition is in a separate xml and added to bcbean.xml.will try to.add in dispatched-servlet.xml and add the results. – Anbu Chinna Nov 06 '16 at 21:31
  • my only concern is when session scope gets defined. since i handle it in filter i have doubt that session scope get defined after 1st request to servlet. I am guessing not sure. – Anbu Chinna Nov 06 '16 at 21:38
  • Adding the configuration in dispatcher-servlet.xml worked. Thank you Moshe. Need to tweak the code and will paste the details. My concern is anotation didn't work nor havining in separate xml and adding in bcbeans.xml didn't work. Might be session scope is not available before the first request.. Thank you Moshe. – Anbu Chinna Nov 07 '16 at 03:50