gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:58:46 25 4
gpt4 key购买 nike

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

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

@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);
.....
}

最佳答案

无法重现。这里已满Minimal, Reproducible Example :

@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 + "]";
}

}
@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());
}

}
@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 确实处于请求范围内。

因此我使用以下 2 个 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]

我的日志显示:

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]

关于java - Spring,无法更改 @RequestScope 服务的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61546619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com