Spring MVC 中整数(最终类)的 CGLib 代理

发布于 2024-10-18 04:31:17 字数 928 浏览 2 评论 0原文

我需要这样的用法:

对于每个请求,我想将 userId 注入 DemoController 但由于是一个没有空构造函数的最终类,我无法注入它。在这种情况下,最佳做法是什么?具有请求范围的服务可以吗?

@Configuration
public class CityFactory{

   @Bean(name = {"currentUserId")
   @Scope(value = WebApplicationContext.SCOPE_REQUEST,proxyMode = ScopedProxyMode.TARGET_CLASS)
   @Autowired
   public Integer getUserId(HttpServletRequest request) {
       return UserUtil.getCurrentUserId(request.getServerName());
   }
}


@RequestMapping("/demo")
@Controller
public class DemoController {

    @Autowired
    Ingeter userId;

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    public ModelAndView helloWorld(@PathVariable("name") String name, Model model) {
        Map<String, Object> myModel = new HashMap<String, Object>();
        model.addAttribute("user", userId);
        return new ModelAndView("v3/test", "m", model);
    }
}

I need such a usage:

For each request I want to inject userId into DemoController But because of being a final class without empty constructor I can not inject it. What is the best practice in such cases? A service with request scope is fine?

@Configuration
public class CityFactory{

   @Bean(name = {"currentUserId")
   @Scope(value = WebApplicationContext.SCOPE_REQUEST,proxyMode = ScopedProxyMode.TARGET_CLASS)
   @Autowired
   public Integer getUserId(HttpServletRequest request) {
       return UserUtil.getCurrentUserId(request.getServerName());
   }
}


@RequestMapping("/demo")
@Controller
public class DemoController {

    @Autowired
    Ingeter userId;

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    public ModelAndView helloWorld(@PathVariable("name") String name, Model model) {
        Map<String, Object> myModel = new HashMap<String, Object>();
        model.addAttribute("user", userId);
        return new ModelAndView("v3/test", "m", model);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-10-25 04:31:20

最好的选择是创建一个名为 UserId 的显式类,该类又包含一个整数。这不仅可以更好地配合 CGLIB 的代理,而且还可以澄清您的设计。

Your best bet is to create an explicit class called UserId, which in turn contains an integer. Not only will this play nicer with CGLIB's proxying, it also clarifies your design.

一指流沙 2024-10-25 04:31:20

您可以使用供应商或提供商

@Configuration
public class CityFactory{

   @Bean
   @Autowired
   public Supplier<Integer> getUserId(HttpServletRequest request) {
       return () -> UserUtil.getCurrentUserId(request.getServerName());
   }
}
@RequestMapping("/demo")
@Controller
public class DemoController {

    @Autowired
    Supplier<Ingeter> getUserId;

You can use Supplier or Provider

@Configuration
public class CityFactory{

   @Bean
   @Autowired
   public Supplier<Integer> getUserId(HttpServletRequest request) {
       return () -> UserUtil.getCurrentUserId(request.getServerName());
   }
}
@RequestMapping("/demo")
@Controller
public class DemoController {

    @Autowired
    Supplier<Ingeter> getUserId;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文