我正在尝试创建一个日志服务来存储两个变量,这两个变量将在http请求的生命周期中使用。问题是我不能更改字段。我尝试了setters、init方法,我可以在调试器中看到值的变化,但在退出方法后,字段为空
唯一的原因是我没有修改相同的对象,但我有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);
.....
}发布于 2020-05-02 01:12:01
无法复制。这是完整的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真正是请求范围的。
因此,我使用以下两个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]https://stackoverflow.com/questions/61546619
复制相似问题