Spring MVC中CGLib Proxy for Integer(Final class)

我需要这样的用法: 对于每个请求,我想将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);
    }
}
    
已邀请:
你最好的办法是创建一个名为
UserId
的显式类,它又包含一个整数。 CGLIB的代理不仅可以更好地发挥作用,还可以澄清您的设计。     
您可以使用供应商或提供商
@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;
    

要回复问题请先登录注册