Internal working of Spring
Spring is popular for its auto-configuration feature. It means it automatically configures our application based on the added dependencies during the project creation. Beans are stored in IoC container. Spring will look for main class with SpringApplication.run method and annotated with @SpringBootApplication. @SpringBootApplication is the combination of three annotations.
- @ComponentScan: Also called user configuration which is first step or phase. It detects and registers spring managed components (@Controller, @Service, @Repository, @RestController etc) with Application Context. @ComponentScan without arguments tells the Spring to scan the current package and the sub-packages.
- @EnableAutoConfiguration: Auto-configuration phase. It enables the Spring to auto-configure the application context. Therefore it automatically creates and registers beans based on jars in the classpath and the beans defined by the user.
- @Configure: Indicates the annotated class declares one or more beans definition or configuration methods.
When the client makes a request, it reaches the dispatcher servlet which is the front controller. Dispatcher servlet consults the HandlerMapping which maps to a specific controller and forwards the request. Controller processes the request by invoking business logic or service and response is generated which is send back to the client.
In SpringMVC, the controller prepares a model object which contains data to be displayed by the view. The controller sends a logical view name to the Dispatcher Servlet. Dispatcher Servlet resolves this to actual view implementation. Once view is resolved, Dispatcher Servlet invokes view to render the response. Finally rendered view is send back to the client.
Comments
Post a Comment