Once we extract the project into our IDE you'll notice couple of classes already created. Let's focus on one of the important class called Application class, which does all the magic to run your spring boot application
package com.artifactsbyrake.restdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestdemoApplication { public static void main(String[] args) { SpringApplication.run(RestdemoApplication.class, args); } } |
- Notice the main method in the above class which is used to launch the spring application
- Also pay attention to the annotation "@SpringBootApplication", this is used to identify the Application class in spring boot
What does spring boot do when it runs the app?
To run an application, spring does the following things on application start-up:
- Create's instance of Spring's ApplicationContext
- Spring loads multiple beans that are needed as per configuration
- Enables functionality to accept command-line args and expose them as spring properties
@SpringBootApplication
Here's a small snippet of code you can see when you expand @SpringBootApplication annotation:
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { |
Understanding the functionality provided by these annotations will help us understand inner workings of spring boot much better:
- @SpringBootConfiguration
- There should be only one annotation like this in entire application and most applications we develop inherit from @SpringBootApplication
- This annotation indicates that a class provides context configuration
- @EnableAutoConfiguration
- Auto-configuration is one of the powerful features of spring boot framework
- At a high level, whenever you add a dependency you'll get certain jar's added to your classpath. Using these jars present in the classpath, spring boot will try to guess and configure beans that you'll likely need.
- For example here are few dependencies and beans that'll be configured
- Dependency: spring-boot-starter-web
- Beans configured:
- DispatcherServlet
- CharacterEncodingFilter
- BeanNameHandlerMapping
- BasicErrorController
- HandlerExceptionResolver
- Dependency: spring-boot-starter-data-jpa
- Beans configured:
- LocalContainerEntityManagerFactoryBean
- JpaTransactionManager
- "spring-boot-autoconfigure-{version}.jar" is the jar that provides support for spring boot auto-configuration
- @ComponentScan
- Enables scanning of spring beans in the package of current class and all it's sub-packages
There are many annotations provided by spring that we'll leverage while building REST API's i'll try to talk about them here once we get to use them.
Happy Coding 👨💻
Comments
Post a Comment