溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring MVC Controller單例陷阱是什么

發(fā)布時間:2021-10-20 16:07:12 來源:億速云 閱讀:103 作者:柒染 欄目:大數(shù)據(jù)

Spring MVC Controller單例陷阱是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

Spring MVC Controller默認是單例的

以下是測試步驟,代碼與結(jié)果.

1. 如果是單例類型類的,那么在Controller類中的類變量應(yīng)該是共享的,如果不共享,就說明Controller類不是單例。以下是測試代碼:

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class ExampleAction {

    private int singletonInt=1;

     @RequestMapping(value = "/test")

     @ResponseBody

     public String singleton(HttpServletRequest request,

             HttpServletResponse response) throws Exception {

         String data=request.getParameter("data");

         if(data!=null&&data.length()>0){

             try{

              int paramInt= Integer.parseInt(data);

             singletonInt = singletonInt + paramInt;

             }

             catch(Exception ex){

                 singletonInt+=10;

             }

         }else{

             singletonInt+=1000;

         }

         return String.valueOf(singletonInt);

    }

}

分別三次請求: http://localhost:8080/example/test.do?data=15

得到的返回結(jié)果如下。

第一次: singletonInt=15

第二次: singletonInt=30

第三次: singletonInt=45

從以上結(jié)果可以得知,singletonInt的狀態(tài)是共享的,因此Controller是單例的。

單例的原因有二:

1、為了性能。

2、不需要多例。

1、這個不用廢話了,單例不用每次都new,當(dāng)然快了。

2、不需要實例會讓很多人迷惑,因為spring mvc官方也沒明確說不可以多例。

  我這里說不需要的原因是看開發(fā)者怎么用了,如果你給controller中定義很多的屬性,那么單例肯定會出現(xiàn)競爭訪問了。

  因此,只要controller中不定義屬性,那么單例完全是安全的。下面給個例子說明下:

package com.lavasoft.demo.web.controller.lsh.ch6;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Created by Administrator on 14-4-9.
 *
 * @author leizhimin 14-4-9 上午10:55
 */
@Controller
@RequestMapping("/demo/lsh/ch6")
@Scope("prototype")
public class MultViewController {
    private static int st = 0;      //靜態(tài)的
    private int index = 0;          //非靜態(tài)
    @RequestMapping("/show")
    public String toShow(ModelMap model) {
        User user = new User();
        user.setUserName("testuname");
        user.setAge("23");
        model.put("user", user);
        return "/lsh/ch6/show";
    }
    @RequestMapping("/test")
    public String test() {
        System.out.println(st++ + " | " + index++);
        return "/lsh/ch6/test";
    }
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI