I am working on an existing code that is using one controller to call a method on the 2nd controller. There are 2 implementations I have seen so far.
1st Implementation
return new Controller().method(request, response);
2nd Implementation
@Autowired
private Controller controller.
return this.controller.method(request, response);
Which is the right implementation, what are the problems if any with either of them.
The mere fact that you need to call a method from another controller reveals a probable design flaw.
With option 1, you lose everything the Spring DI container brought you: namely, that other controller may be instantiated by Spring with some other dependencies wired into it. If you instantiate it yourself, even if it does work at this moment, because you probably have no @Autowired / @Value dependencies, it will break once you’ll add dependencies on other resource(s). Besides, you already have an instance the container built for you, why create others ?