java - Spring MVC RestController scope -


i have 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 access rest api, returns incremented value. learning java , wondering why not return 1 new instance of atomiclong must have been created each time request comes.

no, testcontroller bean singleton. @restcontroller annotation declares spring @component scope default singleton. documented in @scope annotation:

defaults empty string ("") implies scope_singleton.

this means same instance of testcontroller handle every requests. since counter instance variable, same every request.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -