我需要这样的用法:
对于每个请求,我想将 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);
}
}