在 Spring Boot1.5 中,可以使用 @SessionScope 更新实例。但是,当使用 Spring Boot 2.0 时,该值不会更新,它仍然为 null。
会话范围的组件:
@Data
@SessionScope
public class UserDto {
private String firstName;
private String lastName;
}
控制器:
@Controller
public class GreetingController {
@Autowired
UserDto userInfo;
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
userInfo.setFirstName("John");
userInfo.setLastName("Smith");
/**
* There was null value in Spring Boot 2.0.
* In Spring Boot 1.5 it was "John".
**/
System.out.println(userInfo.getFirstName());
return "greeting";
}
}