gpt4 book ai didi

java - 如果我使用 @ControllerAdvice 来填充 @ModelAttribute,我会遇到来自 @ControllerAdvice 的任何冲突吗

转载 作者:行者123 更新时间:2023-11-29 04:36:11 25 4
gpt4 key购买 nike

这是 web 入口点的主 Controller

@Controller
@RequestMapping("/webapp")
public class WebAppController {

@RequestMapping(value = "/home/{authKey}",method = RequestMethod.GET)
String index(@ModelAttribute MyMeta myMeta, Model model){

System.out.println("Token: "+myMeta.getAccessToken());

return "index";
}

@RequestMapping(value = "/config/{authKey}",method = RequestMethod.GET)
String config(@ModelAttribute MyMeta myMeta, Model model){

return "configure";
}
}

现在,如果您查看拦截器,您可以看到我是如何创建@ModelAttribute 的,并查看实现

@Component
@ControllerAdvice
public class SessionInterceptor implements AsyncHandlerInterceptor {

MyMeta myMeta;

...

@ModelAttribute
public MyMeta getTest() {
return this.myMeta;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

...

// parse the key from the request

...

MetaMagicKey metaMagicKey = metaMagicKeyRepo.findKeyByMagicKey(key);

// do work here query my DB and build stuff

...

// assign the queried data built into object
this.myMeta = metaMagicKey.getId().getMyMeta();

return true;
}

我的问题是,我不知道 Springboot 的真正交互作用,所以我担心如果有太多人执行此操作,我可能会发生一些对象交换或某种冲突?确实没有一种干净的方法可以做到这一点,我所做的所有研究都在使用 HttpServletRequest#setAttribute() 和使用 @ModelAttribute 之间徘徊,我喜欢上面选择的路线,因为它在我的方法中非常容易实现.

Springboot 1.4.2 - Java 8

编辑:


我最终尝试的是这个,基于我读过的几页。

我创建了一个新组件:

@Component
@RequestScope
public class HWRequest implements Serializable {

private MyMeta myMeta;

public MyMeta getMyMeta() {
return myMeta;
}

public void setMyMeta(MyMeta myMeta) {
this.myMeta = myMeta;
}

}

然后是我的配置类

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {

UserSessionInterceptor userSessionInterceptor;

@Autowired
public AppConfig(UserSessionInterceptor userSessionInterceptor) {
this.userSessionInterceptor = userSessionInterceptor;
}

@Bean
@RequestScope
public HWRequest hwRequest() {
return new HWRequest();
}

@Bean
public UserSessionInterceptor createUserSessionInterceptor() {
return userSessionInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(createUserSessionInterceptor()).addPathPatterns("/user/**");
}
}

这是我修改的拦截器

@Component
@ControllerAdvice
public class SessionInterceptor implements AsyncHandlerInterceptor {

@Resource
HWRequest hwRequest;

...

@ModelAttribute
public HWRequest getTest() {
return this.hwRequest;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

...

// parse the key from the request

...

MetaMagicKey metaMagicKey = metaMagicKeyRepo.findKeyByMagicKey(key);

// do work here query my DB and build stuff

...

// assign the queried data built into object
this.hwRequest.setMyMeta(metaMagicKey.getId().getMyMeta());

return true;
}

当然还有修改后的 Controller 以满足我的需要

@Controller
@RequestMapping("/user")
public class WebAppUserController {

@RequestMapping(value = "/home/{authKey}",method = RequestMethod.GET)
String index(@ModelAttribute HWRequest request, Model model){

return "index";
}

@RequestMapping(value = "/config/{authKey}",method = RequestMethod.GET)
String config(@ModelAttribute HWRequest request, Model model){

return "configure";
}

}

根据我读过的所有文档,这应该可行,但也许我遗漏了一些东西,因为拦截器仍然是一个单例。也许我遗漏了什么?

最佳答案

myMeta 变量表示单例 bean 中的状态。当然它不是线程安全的,不同的用户会发生冲突。永远不要将任何应用程序状态存储在单例 bean 中。

如果您想为每个请求存储一些状态,请使用 Spring 的请求范围。这意味着 creating separate bean just for storing state annotated with @RequestScope annotation

对编辑的 react :

这个 bean 注册可以删除,因为它已经用 @Component 注解注册到 Spring IoC 容器中:

@Bean
@RequestScope
public HWRequest hwRequest() {
return new HWRequest();
}

您的 AppConfig 中不需要的另一部分是 Autowiring UserSessionInterceptor bean 并再次将其注册为 bean。删除那个。由于该 bean 正在 Autowiring ,因此它显然已经在 IoC 容器中,因此无需再次注册。

另一个令人困惑的部分是 session 的命名。当您处理 @RequestScope 而不是 @SessionScope 时,我建议您将类的命名更改为 request (例如 RequestInterceptor)。 session 与请求是非常不同的野兽。

否则看起来它可以工作并且应该是线程安全的。

关于java - 如果我使用 @ControllerAdvice 来填充 @ModelAttribute,我会遇到来自 @ControllerAdvice 的任何冲突吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41404164/

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