首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring,无法更改@RequestScope服务的值

Spring,无法更改@RequestScope服务的值
EN

Stack Overflow用户
提问于 2020-05-02 00:33:05
回答 1查看 314关注 0票数 0

我正在尝试创建一个日志服务来存储两个变量,这两个变量将在http请求的生命周期中使用。问题是我不能更改字段。我尝试了setters、init方法,我可以在调试器中看到值的变化,但在退出方法后,字段为空

唯一的原因是我没有修改相同的对象,但我有RequestScope...

代码语言:javascript
复制
@Service
@RequestScope
public class LogService {

    private String id;
    private String type;

    public void init(String id, String type) {
       this.id = id;
       this.type = type;
    }
}


@RestController
@RequestMapping("/")
@AllArgsConstructor
public class Controller {

    private OtherService otherService;
    private LogService logService;

    @PostMapping(value = "create", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity create(@RequestBody BodyObject body) {

       /* in the debugger i can see that the values are set in the object but after getting out of the logService.init the object the variables are null */
        logService.init("service", body.getId());

         /* here both fields are null and also inside other services using the  logService */
         otherService.execute(body);
        .....
    }
EN

回答 1

Stack Overflow用户

发布于 2020-05-02 01:12:01

无法复制。这是完整的Minimal, Reproducible Example

代码语言:javascript
复制
@Service
@RequestScope
public class LogService {

    private String id;
    private String type;

    @SuppressWarnings("hiding")
    public void init(String id, String type) {
        this.id = id;
        this.type = type;
    }

    public String getId() {
        return this.id;
    }

    public String getType() {
        return this.type;
    }

    @Override
    public String toString() {
        return "LogService[id=" + this.id + ", type=" + this.type + "]";
    }

}
代码语言:javascript
复制
@Service
public class ScopeService {

    @Autowired
    private LogService logService;

    public void test() {
        System.out.println("ScopeService: " + this.logService);
        System.out.println("  id=" + this.logService.getId() + ", type=" + this.logService.getType());
    }

}
代码语言:javascript
复制
@RestController
public class ScopeController {

    @Autowired
    private ScopeService scopeService;
    @Autowired
    private LogService logService;

    @GetMapping("/dmz/scope")
    public String create(@RequestParam String id, @RequestParam String type) throws InterruptedException {
        this.logService.init(id, type);
        Thread.sleep(10000);
        this.scopeService.test();
        return this.logService.toString();
    }

}

Thread.sleep()将测试并行web请求处理,以确保LogService真正是请求范围的。

因此,我使用以下两个URL,它们在10秒内提交:

http://localhost:8080/dmz/scope?id=1&type=TEST1

http://localhost:8080/dmz/scope?id=2&type=TEST2

它们返回的页面显示:LogService[id=1, type=TEST1]

和:LogService[id=2, type=TEST2]

我的日志显示:

代码语言:javascript
复制
2020-05-01 13:10:29.117 DEBUG 5888 --- [nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter      : Before request [GET /dmz/scope?id=1&type=TEST1]
2020-05-01 13:10:30.312 DEBUG 5888 --- [nio-8080-exec-2] o.s.w.f.CommonsRequestLoggingFilter      : Before request [GET /dmz/scope?id=2&type=TEST2]
ScopeService: LogService[id=1, type=TEST1]
  id=1, type=TEST1
2020-05-01 13:10:39.162  INFO 5888 --- [nio-8080-exec-1] shared.EventLogger                       : EVENT: ServletRequestHandledEvent
2020-05-01 13:10:39.162 DEBUG 5888 --- [nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter      : REQUEST DATA : GET /dmz/scope?id=1&type=TEST1]
ScopeService: LogService[id=2, type=TEST2]
  id=2, type=TEST2
2020-05-01 13:10:40.319  INFO 5888 --- [nio-8080-exec-2] shared.EventLogger                       : EVENT: ServletRequestHandledEvent
2020-05-01 13:10:40.320 DEBUG 5888 --- [nio-8080-exec-2] o.s.w.f.CommonsRequestLoggingFilter      : REQUEST DATA : GET /dmz/scope?id=2&type=TEST2]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61546619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档