Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC RestController scope

I have the following Spring controller:

package hello;

import java.util.concurrent.atomic.AtomicLong;

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

@RestController
public class TestController {
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/test")
    public String test() {
        long val = counter.incrementAndGet();
        return String.valueOf(val);
    }
}

Each time I access the REST API, it returns an incremented value. I am just learning Java and I am wondering why it does not always return 1 as a new instance of AtomicLong must have been created each time the request comes.

like image 398
Babu James Avatar asked Oct 15 '15 07:10

Babu James


People also ask

What is the scope of RestController?

@RestController annotation declares a Spring @Component whose scope is by default SINGLETON . This is documented in the @Scope annotation: Defaults to an empty string ("") which implies SCOPE_SINGLETON. This means that it will be the same instance of TestController that will handle every requests.

Can we use RestController in Spring MVC?

Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.

What is the scope of @controller in Spring?

Spring MVC controllers are singleton by default and any controller object variable/field will be shared across all the requests and sessions. If the object variable should not be shared across requests, one can use @Scope("request") annotation above your controller class definition to create instance per request.

What is the difference between @controller and @RestController in Spring MVC?

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.


2 Answers

No, the TestController bean is actually a singleton. @RestController annotation declares a Spring @Component whose scope is by default SINGLETON. This is documented in the @Scope annotation:

Defaults to an empty string ("") which implies SCOPE_SINGLETON.

This means that it will be the same instance of TestController that will handle every requests. Since counter is an instance variable, it will be same for every request.

like image 193
Tunaki Avatar answered Oct 17 '22 08:10

Tunaki


A @RestController is not created for each request, it remains the same for every request. So your counter keeps its value and is incremented each time.

like image 40
Gaël J Avatar answered Oct 17 '22 08:10

Gaël J