Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

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:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Testing session scoped beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    AdminServiceConfig.java

    Code:
    @Configuration
    public abstract class AdminServiceConfig {
    	
    	@Bean(scope = DefaultScopes.SESSION)
    	@ScopedProxy(proxyTargetClass = false)
    	public BenutzerkontoServiceImpl serviceBenutzerkonto() {
    		BenutzerkontoServiceImpl service = new BenutzerkontoServiceImpl();
    		service.setLockingService(serviceLocking());
    		service.setMaxId(maxIdDAO());
    		service.setUserDao(userDAO());
    		service.setPasswordsDao(passwordsDAO());
    		service.setFunktionsRechteDao(funktionsRechteDAO());
    		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 org.springframework.config.java-1.0.0.M4.jar and org.springframework.config.java-sources-1.0.0.M4.jar 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.

    TestBenutzerkontoServiceImpl.java:

    Code:
    @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$
        }
    	
    	@Autowired
    	public void setBenutzerkontoService(BenutzerkontoService BenutzerkontoService){
    		this.BenutzerkontoService = BenutzerkontoService;
    	}
    }
    Last edited by t.heuer; Jan 30th, 2009, 08:18 AM.

  • #2
    Hmm,

    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:

    Code:
    XmlWebApplicationContext t = new XmlWebApplicationContext();
    t.setConfigLocation("file:C:/.../applicationContext.xml");
    t.setServletContext(new MockServletContext(""));
    			
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    t.refresh();
    t.getBeanFactory().registerScope( "session", new SessionScope());
    t.registerShutdownHook();
    		
    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.

    Comment


    • #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);

      Comment


      • #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.

        Comment


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

          Comment


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

            http://forum.springsource.org/showth...d=1#post225952

            Everything workes as expected for me now.

            Comment

            Working...
            X