以下代码中的原子整数是否在不同的REST调用之间共享?如果它是静态的怎么办?
public class GreetingController {
private static final String template = "Hello Docker, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value="name",
defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
如果控制器是singleton,则为共享。
因为这看起来像Spring MVC (您没有说),并且由于默认情况下@Controller
类是单例,所以答案是:
是,原子整数在不同的REST调用之间共享。
不必是static
。