Announcement Announcement Module

JavaConfig forum decommissioned in favor of Core Container

As described at

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at
See more
See less
Testing session scoped beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Testing session scoped beans

    Hi, I'm working on a prototype with a GWT web presentation layer and a spring backend. To connect both I use GWT Server Library. I configured everything with XML and it worked perfectly fine. Since I found out about spring javaconfig i want to use this for configuration. But I get problems with session scoped beans.

    On runtime I get the following warning, although communication works later on.:

    [WARN] Context initialization failed
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/benutzerkonto-servlet.xml]: Cannot resolve reference to bean 'serviceBenutzerkonto' while setting bean property 'mappings' with key [TypedStringValue: value [/stammdaten.srv], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'serviceBenutzerkonto': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

    When running my Junit Tests I get the following error:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '...TestBenutzerkontoServiceImpl': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void ...TestBenutzerkontoServiceImpl.setBenutzerkontoSe rvice(...client.interfaces.BenutzerkontoService); nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [...client.interfaces.BenutzerkontoService] is defined: Unsatisfied dependency of type [interface ...interfaces.BenutzerkontoService]: expected at least 1 matching bean

    My Configuration:

    public abstract class AdminServiceConfig {
    	@Bean(scope = DefaultScopes.SESSION)
    	@ScopedProxy(proxyTargetClass = false)
    	public BenutzerkontoServiceImpl serviceBenutzerkonto() {
    		BenutzerkontoServiceImpl service = new BenutzerkontoServiceImpl();
    		return service;
    If I remove the session scope, everything is workling withoiut warnings or errors.

    Does anybody have an Idea what's going on?

    I'm importing in the classpath and plus the files in the lib folder of javaconfig 1.0.0.M3

    To get the Tests working with the old XML Configuration I had to extend the AbstractContextLoader, to provide the test with a web Context. If necessary I can also show the code here, but I guess there is a problem with the proxying and I'm probably not using it correct.

    @ContextConfiguration(locations = { "applicationContext.xml" }, loader=TestWebSessionContextLoader.class)
    public class TestBenutzerkontoServiceImpl extends AbstrServiceTest{
    	private BenutzerkontoService BenutzerkontoService;
    	protected IDataSet getDataSet() throws Exception {
            return new FlatXmlDataSet(new FileInputStream("stammdaten.xml")); //$NON-NLS-1$
    	public void setBenutzerkontoService(BenutzerkontoService BenutzerkontoService){
    		this.BenutzerkontoService = BenutzerkontoService;
    Last edited by t.heuer; Jan 30th, 2009, 08:18 AM.

  • #2

    OK, I tryed something else. Now I Try to access the session scoped bean from above. Since I want to use the same config in Rich Client without tomcat environment I'll try to create a web context on my own. I found this code:

    XmlWebApplicationContext t = new XmlWebApplicationContext();
    t.setServletContext(new MockServletContext(""));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    t.getBeanFactory().registerScope( "session", new SessionScope());
    service = (SpringKomponente) t.getBean("SpringKomponente");
    But I get the error:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'SpringKomponente' defined in file [C:..applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'session'

    Any Ideas?

    I think I have to use something else than JavaConfigApplicationContext, since I read a XML File.

    Using only JavaConfigApplicationContext dosen' work either, since I want to configure the db connection in a dynamic file, so these beans are not in the JavaConfig class.
    Last edited by t.heuer; Feb 3rd, 2009, 08:55 AM.


    • #3
      Are you aware of JavaConfigWebApplicationContext? Try using that. As for coordinating access to your XML-defined beans, consider using @ImportXml or creating a hierarchical relationship between contexts with sjcContext.setParent(xmlContext);


      • #4
        Thanks alot. That was the hint I needed. I was aware of JavaConfigWebApplicationContext and meant this one in the post above. I didn't know that I can give this context a Parent.

        But it is a little bit weird, that I have to create the javaconfig Context and inject a Parent, to access a bean in the javaconfig Context and this is only necessary if I use scoped session beans.

        It would be nice if i could just create an ApplicationContext of a bean.xml which contains children of javaconfig and ask this Context for beans.


        • #5
          Consider using ConfigurationPostProcessor for this. Take a look at the reference documentation for usage instructions.


          • #6
            This Problem was solved with another issue about session scoped beans


            Everything workes as expected for me now.