Must Spring MVC Classes be Thread-Safe

来源: http://stackoverflow.com/questions/16795303/must-spring-mvc-classes-be-thread-safe


Given

@Controllerpublic class MyController {
    @RequestMapping(value = "/index")
    public String respond() {
        return "index";
    }}

Spring will create an instance of MyController. This is because Spring parses your configuration, <mvc:annotation-driven>, sees @Controller (which is like @Component) and instantiates the annotated class. Because it sees @RequestMapping as well, it generates a HandlerMapping for it, see the docs here.

Any HTTP requests the DispatcherServlet receives will be dispatched to this controller instance through the HandlerMapping registered before, calling respond() through java reflection on that instance.

If you have instance fields like

@Controllerpublic class MyController {
    private int count = 0;
    @RequestMapping(value = "/index")
    public String respond() {
        count++;
        return "index";
    }}

count would be a hazard, because it might be modified by many threads and changes to it might be lost.

You need to understand how Servlet containers work. The container instantiates one instance of your Spring MVC DispatcherServlet. The container also manages a pool of Threads which it uses to respond to connections, ie. HTTP requests. When such a request arrives, the container picks a Thread from the pool and, within that Thread, executes the service() method on the DispatcherServletwhich dispatches to the correct @Controller instance that Spring registered for you (from your configuration).

So YES, Spring MVC classes must be thread safe. You can do this by playing with different scopes for your class instance fields or just having local variables instead.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误通常是因为你传递给函数的矩阵不是二维的。请检查你的代码,确保你传递给函数的矩阵是一个二维的 NumPy 数组或矩阵。如果你不确定如何将一个数组转换为二维矩阵,请使用 `reshape` 函数将其转换为二维形式。例如: ```python import numpy as np # 创建一个一维数组 a = np.array([1, 2, 3]) # 将其转换为二维矩阵 b = a.reshape(1, -1) # 现在 b 是一个二维矩阵 print(b) ``` ### 回答2: "ValueError: matrix must be 2-dimensional"的错误是指输入的矩阵必须是二维的。这个错误通常是出现在使用矩阵操作时,当我们尝试在一个不是二维矩阵的对象上执行矩阵运算时,就会出现这个错误。 解决这个错误的方法是确保我们传递给矩阵运算的对象是一个二维矩阵。我们可以使用numpy库中的函数将一个一维的数组转换为二维矩阵。例如,我们可以使用numpy的reshape()函数将一个一维数组转换为具有指定形状的二维矩阵。 另外,确保在对矩阵进行操作之前,我们可以通过调用numpy库中的函数ndim来检查矩阵的维度。如果维度小于2,则需要将其转换为二维矩阵。 下面是一个以Python为例的示例代码,用于解决"ValueError: matrix must be 2-dimensional"的问题: ```python import numpy as np # 一维数组 arr = np.array([1, 2, 3, 4]) # 将一维数组转换为二维矩阵 matrix = np.reshape(arr, (1, len(arr))) # 检查矩阵的维度 if matrix.ndim < 2: raise ValueError("matrix must be 2-dimensional") # 执行矩阵运算 # ... ``` 在这个示例中,我们首先将一个一维数组arr使用np.reshape()函数转换为一个具有1行和len(arr)列的二维矩阵。然后,我们使用ndim属性检查矩阵的维度是否小于2,如果小于2就抛出一个ValueError异常。最后,我们可以继续执行所需的矩阵运算。 ### 回答3: ValueError: matrix must be 2-dimensional 是一个Python中的错误提示,意思是矩阵必须是二维的。 在Python中,矩阵通常被表示为二维数组或列表的列表。也就是说,如果我们想创建一个矩阵,我们需要在数组或列表中嵌套其他数组或列表。 这个错误的原因可能是我们尝试使用一维数组或列表来表示矩阵,而不是正确的二维数组或列表。 为了解决这个错误,我们需要确保我们传递给关于矩阵的函数或方法的参数是一个二维数组或列表。 下面是一个例子: ```python matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # 正确的二维数组表示矩阵 # 现在我们可以使用这个矩阵进行计算或其他操作 ``` 如果我们尝试使用一维数组或列表来创建矩阵,就会出现上述的错误: ```python matrix = [1, 2, 3] # 错误!一维数组无法表示矩阵 # 这会导致 ValueError: matrix must be 2-dimensional 错误 ``` 所以,为了避免出现 ValueError: matrix must be 2-dimensional 错误,我们需要确保矩阵被正确表示为二维数组或列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值