簡體   English   中英

DispatcherServlet是否為每個用戶在內部創建控制器和方法的新實例?

[英]Does the DispatcherServlet creates new instance of controller and methods inside for each user?

大家好。 我的應用程序中有控制器:

        @Controller
        public class LoginSearchController {

            //List to store data from resultHTML method
            List<Cars> listCars = null;


            @Autowired
            private EducationWebServiceInterface educationWebService;

                //This method render jsp page for user and create model for resultHTML
                @RequestMapping(value="/search", method=RequestMethod.GET)
                public String search(FormBackingObjectSearch fbos, Model model) {   
                        model.addAttribute("fbosAttributes", fbos);

                    return "search";        
                }   

               //   This method get name form form back object and use it for fetchCarsByNameService method
              // After if something found it stores it in listofSerchObjects and after listofSerchObjects stores it in listForm List
                @RequestMapping(value="/result", method=RequestMethod.GET)
                public String resultHTML(@RequestParam String name,
                                         @ModelAttribute("fbosAttributes") FormBackingObjectSearch fbos, 
                                         BindingResult bindingResut, Model model) throws Exception {


                    listCars = new ArrayList<Cars>();
                    List<Cars> listofSerchObjects = null;

                    listofSerchObjects = educationWebService.fetchCarsByNameService(dateConvertation(fbos.getName()));
                        model.addAttribute("findAttributes", listofSerchObjects);   
                        listCars.addAll(listofSerchObjects);

                    return "search";
                }


                  //This method fetch data from listForm to make Excel document form the list
                @RequestMapping(value="/result.xls", method=RequestMethod.GET)
                public String resultXLS(Model model) throws Exception {

                        if(listCars == null || listCars.size() == 0) {
                            throw new NullPointerException("ArrayList<FormDate> formDateList is empty list");
                        } else {            
                            model.addAttribute("findAttributesXls", listForm);
                        }               
                    return "xlspage";
                }
        }

控制器中會發生什么:

用戶進入要Search的汽車的Search頁面填充名稱(在我的情況下),然后單擊“ FIND按鈕

->請求轉到/result resultHTML方法從服務層調用fetchCarsByNameService ,以將與特定名稱對應的數據獲取到listofSerchObjects並將其呈現在jsp頁面上。 listofSerchObjects具有來自fetchCarsByNameService數據后,它將所有數據添加到其他列表listCars

->下一步,當所有數據/result.xls現在jsp頁面上時,出現一個按鈕SAVE AS EXCEL ,發生了什么:當用戶單擊此按鈕(如果用戶也想要)時,它進入/result.xls 並從 listCars 所有數據將其保存在Excel文件中。

我很好奇,如果多個用戶將在我的Web應用程序中的不同計算機上在同一jsp頁面上工作, 我的listCars將如何運行。 我很害怕它會存儲從數據庫中提取的最后一個數據,如果user1希望將其呈現在Excel文件中,那么如果user2進行最后搜索,他將擁有來自user2的數據。 你認為呢??? 請給我建議。 謝謝

由於您將listCars實例設為類級別變量,因此它將在每個用戶之間共享。 由於每個用戶(和請求)都將擁有自己的線程,因此該類級別的變量將在所有用戶之間共享,並且取決於請求的傳入速度,您的數據可能會被破壞。

listCars已損壞,甚至可能偶爾拋出ConcurrentModificationException

以下是該問題的解決方法。

  1. listCars為局部變量。

    要么

  2. @Scope("session")注釋您的Controller

這是一個出色的博客 ,它描述了Controller Scope及其優缺點。

@Controller是一個Spring bean,它只會被容器實例化一次。 將與特定請求相關的所有變量保留在堆棧上(在方法內部聲明)。

listCars將在應用程序的所有用戶之間共享。

/ result和/result.xls的兩個調用彼此完全獨立。 您需要再次查找汽車列表或將listCars作為參數傳遞給此方法。 您還可以將listCars存儲到用戶的會話中,並在result.xls調用期間進行檢索,但這確實會污染不需要的數據,這些數據可能永遠不會被使用。

根據listCars可能有多大以及fetchCarsByNameService有多昂貴,應確定是將列表作為參數傳遞回去,還是僅傳遞與/ result相同的參數並重新運行該方法。 如果操作昂貴,我可能傾向於重新運行該方法並在fetchCarsByNameService上具有某種緩存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM