首页 > 解决方案 > Spring boot Controller 方法也是单例的吗?

问题描述

我正在编写一个 Spring Boot 应用程序,我对我的控制器类有疑问。我阅读了文档,发现控制器默认是单例的,所以我决定将我的用户信息保存在数据库中并通过会话保存。我使用 spring boot sesison 管理 JDBC。我的问题是,如果一个方法也是单例。我知道这似乎是一个愚蠢的问题,但我开始怀疑自己。如果多个用户使用会话登录,我的代码是否会失败,我会感到困惑。另外,我的方法中可以没有任何变量,还是应该将我在这里的逻辑移到其他地方。我真的不知道。

Java 控制器方法:

@RequestMapping(value = "/edit")
    public ModelAndView editTab(@RequestParam(value = "currentAppcode", required = false, defaultValue = "Appcode") String currentAppcode, 
                                @RequestParam(value = "currentAcro", required = false, defaultValue = "Arco") String currentAcro, 
                                @RequestParam(value = "currentAppname", required = false, defaultValue = "Appname") String currentAppname, 
                                HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        private List<Some_Object> allQueryConfig = new ArrayList<Some_Object>();

        session.setAttribute("SELECTED_APP",queryService.findDtoById(currentCode)); 
        session.setAttribute("ALL_SERVERS", queryService.serverWork() );

        allSystems.clear();

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentPrincipalName = authentication.getName();

        try {
            OPTApplication selected_app = (OPTApplication) session.getAttribute("SELECTED_APP");
            rc.setProxy();

            if (rc.validateUser(currentPrincipalName, selected_app.getSys_id()) != 0 || queryService.isAuditor(currentPrincipalName)) { 

                session.setAttribute("GP_SELECTED", apiRunner.runGPData(selected_app.getNumber()));


                allQueryConfig = queryService.getNonDistinctServerInfo(); 


                modelAndView.setViewName("create");

                for (AppSelectorTier current : allQueryConfig) {
                    allSystems.add(current.getSystem());

                }
            } else {
                modelAndView.setViewName("forbidden");
            }

        } catch (Exception e) {

            e.printStackTrace();
            modelAndView.setViewName("test");

        }
        modelAndView.addObject("currentAppName", "Application:  " + currentAppname);
        modelAndView.addObject("ary4", queryService.getServerInfo());
        modelAndView.addObject("adid", currentPrincipalName);   
        modelAndView.addObject("hasAccess", false);

        return modelAndView;
    }

另一个例子:

    public @ResponseBody List<SelectorDTO> getGPdataTaddm(@RequestBody TestApp mTestApp, HttpServletRequest request, HttpSession session)
            throws SQLException {
        GroupPatternDTO gp_selected = (GroupPatternDTO) session.getAttribute("GP_SELECTED");

        session.setAttribute("QUERY_CONFIG_DATA", new QueryConfigData());
        QueryConfigData query_config_data = (QueryConfigData) session.getAttribute("QUERY_CONFIG_DATA");

        List<SelectorDTO> inProgressInfo = queryService.getInfoFromInProgress(gp_selected.getCode());
        if (inProgressInfo.size() != 0) {
            gp_selected.setSels(inProgressInfo);


        } else {

            for (SelectorDTO current : gp_selected.getSels()) {
                String objectPassed = current.getDescription().replaceAll("[^a-zA-Z0-9]", "");
                System.out.println("First match: ----------------------------------------------------------------------"
                        + objectPassed);

                if (queryService.getTierTypeFromObj(objectPassed).size() != 0) {
                    AppSelectorTier selectorObj = queryService.getTierTypeFromObj(taddmObjectPassed).get(0);
                    query_config_data.setName(selectorObj.getName());
                    query_config_data.setSystem(selectorObj.getSystem());
                    query_config_data.setMqlList(selectorObj.getMqllisting());
                    query_config_data.setTag1(selectorObj.getTagname_one());
                    query_config_data.setTag2(selectorObj.getTagname_two());
                    query_config_data.setMqlSelector(selectorObj.getMqllisting());
                    query_config_data.setParseInstruc(selectorObj.getParseinstruction());
                    query_config_data.setTaddmObject(selectorObj.getTaddmobj());



                }

            }
        }
        session.setAttribute("GP_SELECTED", gp_selected);
        session.setAttribute("QUERY_CONFIG_DATA", query_config_data);
        return gp_selected.getSels();

    }

我不确定这是否是一个好的做法,或者对于我的用例来说这是否可以?

标签: javaspring-bootspring-mvcspring-security

解决方案


推荐阅读