Home > Java > No Scope registered for scope request

No Scope registered for scope request

by paul on June 5, 2009

Recently I was working with some code which defined beans as request scope. Junit tests failed with the following exception while trying to initialize the application context:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: …; nested exception is java.lang.IllegalStateException: No Scope registered for scope ‘request’

I found the solution by searching around (unfortunately I didn’t record the sources). Basically, you can register the required scopes as shown below:


import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.SessionScope;

public class SampleTest extends AbstractDependencyInjectionSpringContextTests {

    @Override
    protected void prepareTestInstance() throws Exception {
        context.getBeanFactory().registerScope("session", new SessionScope());
        context.getBeanFactory().registerScope("request", new RequestScope());
        MockHttpServletRequest request = new MockHttpServletRequest();
        attributes = new ServletRequestAttributes(request);
        RequestContextHolder.setRequestAttributes(attributes);

        super.prepareTestInstance();
    }
}

{ 4 comments… read them below or add one }

abalyan November 10, 2009 at 3:53 am

is context of type ApplicationContext? I can’t find method getBeanFactory for it

paul November 10, 2009 at 10:58 pm
Robert Mark Bram April 6, 2010 at 5:13 pm

I was getting this error too. I don’t understand the error or how your solution works. How are you initialising “context” and “attributes”?

I tried initialising them like so:
ConfigurableApplicationContext context = getContext(null);
ServletRequestAttributes attributes = new ServletRequestAttributes(request);

However, it didn’t solve my problem, so I kept looking and found this post in mail-archives.apache.org: http://bit.ly/bO5HdU

It says I needed to define my bean in both ApplicationContext and faces-config.

Still don’t understand it, but I have green bar and need to move on..

Rob
:)

Trung Hoang July 19, 2010 at 9:00 pm

For those using junit 4 and spring 3, i adapted Paul’s code:

@Before
public void setup() {
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
configurableBeanFactory.registerScope(“session”, new SessionScope());
configurableBeanFactory.registerScope(“request”, new RequestScope());
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(attributes);
}

Be sure not to autowire your controller. Just use applicationContext.getBean() in the setup or in your test method.

Leave a Comment

Previous post:

Next post: