How exactly does the proxyMode parameter work in the @Scope annotation?

29 Views Asked by At

I have a simple class HomeController (singleton) and a HomeService (prototype, proxyMode = ScopedProxyMode.TARGET_CLASS).

@Controller
public class HomeController {
    private final HomeService homeService;

    public HomeController(HomeService homeService) {
        this.homeService = homeService;
    }

    @GetMapping("/")
    @ResponseBody
    public String getHome() {
        homeService.ping();
        return "home";
    }
}


@Service
@Scope(scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class HomeService {
    private static final Logger log = LoggerFactory.getLogger(HomeService.class);

    @PostConstruct
    public void init() {
        log.info("init HomeService");
    }

    public void ping(){
        log.info("ping ... ");
    }
}

Why if I make 2 requests do I get the following output to the console?

init HomeService
ping ... 
ping ... 

init HomeService
ping ... 
ping ... 

Examining the previous answers on StackOverflow, I expect that every access to proxy_HomeService, causes it to access ApplicationContext and therefore I should get 4 different HomeService, I expect the following output:

init HomeService
ping ... 
init HomeService
ping ... 

init HomeService
ping ... 
init HomeService
ping ... 
0

There are 0 best solutions below