简体   繁体   English

Spring RESTful Web服务和bean的“请求”和“会话”范围

[英]Spring RESTful Web service and bean “request” and “session” scope

I am using the pure example code of simple REST service from the spring guide as a base: http://spring.io/guides/gs/rest-service/ 我将使用Spring指南中的简单REST服务的纯示例代码作为基础: http : //spring.io/guides/gs/rest-service/

I have added single Bean configuration: 我添加了单个Bean配置:

@Configuration
public class Config {

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_REQUEST)

    public RequestData requestHelper() {
        return new RequestData();
    }     

}

Then my modified controller looks as follows: 然后,我修改后的控制器如下所示:

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        System.out.println(applicationContext.getBean(RequestData.class));

        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

and I am getting 而我得到

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

as the result of calling "/greeting" 称为“ / greeting”的结果

I have read some description here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html however I am still confused. 我在这里阅读了一些描述: http : //docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html但是我仍然很困惑。

they write: "The request, session, and global session scopes are only available if you use a web-aware Spring ApplicationContext implementation". 他们写道:“只有在使用Web感知的Spring ApplicationContext实现时,请求,会话和全局会话作用域才可用”。

Does it mean that "AnnotationConfigApplicationContext" which I am using is not allowed in such case? 这是否表示在这种情况下不允许使用“ AnnotationConfigApplicationContext”? Am I forced to use some xml configuration instead? 我是否被迫使用一些xml配置?

The quote 报价单

web-aware Spring ApplicationContext implementation Web感知的Spring ApplicationContext实现

refers to an appropriate subclass of WebApplicationContext . 引用WebApplicationContext的适当子类。 You're instantiating a AnnotationConfigApplicationContext which is not a subtype of WebApplicationContext and which does not register the SESSION and REQUEST scopes. 您正在实例化一个AnnotationConfigApplicationContext ,它不是WebApplicationContext的子类型,并且没有注册SESSIONREQUEST范围。

It also makes very little sense to create a brand new ApplicationContext in your @RestController . @RestController创建全新的ApplicationContext也几乎没有意义。 The @RestController object is already a bean within a Spring WebApplicationContext . @RestController对象已经是Spring WebApplicationContext的bean。 Just add your new request scoped @Bean to that context and autowire into your controller. 只需将范围为@Bean的新请求添加到该上下文中,并自动装配到控制器中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM