- Cloud-Native Applications in Java
- Ajay Mahajan Munish Kumar Gupta Shyam Sundar
- 192字
- 2021-06-24 19:07:16
Writing a Spring Boot application class
This class contains the main method where the execution starts. This main method will bootstrap the Spring Boot application, look at the configurations, and start the respective bundled containers such as Tomcat if executing web services:
package com.mycompany.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@SpringBootApplication
public class ProductSpringApp {
publicstaticvoid main(String[] args) throws Exception {
SpringApplication.run(ProductSpringApp.class, args);
}
}
Note the annotation called @SpringBootApplication.
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan, which do the following:
- @Configuration: This is a core Spring annotation. It tells Spring that this class is a source of the Bean definitions.
- @EnableAutoConfiguration: This annotation tells Spring Boot to guess how you will want to configure Spring, based on the JAR dependencies that you have added. We have added the starter web and hence the application will be considered to be a Spring MVC web application.
- @ComponentScan: This annotation tells Spring to scan for any components, for example, the RestController that we are going to write. Note the scan happens in current and child packages. Hence, the class having this component scan should be at the top of the package hierarchy.