【问题标题】:Spring boot Annotations keeps http data in the variablesSpring boot Annotations 将 http 数据保存在变量中
【发布时间】:2016-07-30 14:11:48
【问题描述】:

我的弹簧靴有问题。 它将数据保存在一个被注释的类中:

@RestController、@Service、@Component。

我无法保留任何数据,因为它会像会话一样与其他人共享。

例子:

@RestController
public class controller {

    int x = 65;

    @RequestMapping(value = "/set/{number}", method = RequestMethod.GET)
    public int view(@PathVariable("number") int number) {
        x = number;
        return x;
    }       

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public int view2() {
        return x;
    }
}

第一次调用: localhost:80802/get 结果:65。

第二次通话: localhost:8080/set/5 结果:5

第三次调用: localhost:8080/get Result:5

第三次调用结果假定为:65

这种情况也发生在我提到的其他注释中。

为什么它使用@RestController 类作为一种单例? 我无法在带注释的类中存储任何内容?

谢谢。

【问题讨论】:

    标签: spring-boot annotations spring-restcontroller spring-boot-maven-plugin


    【解决方案1】:

    您可以使用@Scope 注释,如here 所述。例如:

    @Scope(value="session", proxyMode =ScopedProxyMode.TARGET_CLASS)
    @RestController
    public class controller {
    ....
    }
    

    【讨论】:

    • 我试过了,它仍然将数据保存在“x”变量中。我希望在下一次 http 调用中数据不会存储在该类中...
    • 我认为,您必须通过将spring.aop.proxy-target-class=true 添加到application.properties 文件来启用类代理。
    • 我也试过了,但在“set/5”调用之后,我仍然没有在“get”调用中得到初始数字 65。
    猜你喜欢
    • 2019-07-26
    • 2020-02-12
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多