In the part 1 of #SpringBoot tutorial we got the gist of the Spring Boot, in part 2 we discussed dependency injection. Now in this tutorial we will create a basic Spring Boot project from scratch.
You can STS (Spring Tool Suite) to create the project. STS is an Eclipse-based development environment. It is an IDE to develop Spring applications. It provides a ready-to-use environment to implement, run, deploy, and debug the application.
You can download the Spring Tool Suite from https://spring.io/tools3/sts/all. It's pretty simple to install.
After the installation, create a workspace and create a new project. While creating a new project select 'Spring Starter Project' (you will not get this option in eclipse). Select the service URL as https://start.spring.io/ while creating the project it will download the blank project from the official website (while doing this make sure you are connected to the Internet). Add any name to the project, you can select maven or gradle, enter other required details and click on next. Add the dependencies you need and click on finish.
Now let's assume, you are using eclipse. In eclipse we don't have an option to create a 'Spring Starter Project', for this we need to go to the official website of https://start.spring.io/ to create the project.
As you can see I am creating a maven project using Spring Boot 2.6.3. I have selected Java version as 8, if you want you can select Java 11 or 17. As of now I have not added any dependency. After clicking on the 'Generate' button, it will download the zip file. Unzip the project and import it in eclipse.
After importing you will find a java class annotated with @SpringBootApplication. In my case it's SpringbootApplication. This is the main class from where our spring boot application is launched.
For testing add any log or sysout in the main method, and run this class as a normal Java application.
To download this test project from git CLICK HERE.
What is the purpose of @SpringBootApplication?
- The entry point of the spring boot application is the class contains @SpringBootApplication annotation and the main method.
- @SpringBootApplication annotation will enable a host of features like Java-based Spring configuration, component scanning etc.
- @SpringBootApplication is a 3-in-1 annotation that combines the functionality of @Configuration, @ComponentScan, and @EnableAutoConfiguration.
- @EnableAutoConfiguration enables Spring Boot’s auto-configuration mechanism (i.e creating beans automatically by scanning the classpath).
- Typically, in a Spring application, @Component, @Configuration, @Service, @Repository are specified on classes to mark them as Spring beans.
- @ComponentScan tells Spring Boot to scan the current package and its sub-packages in order to identify annotated classes and configure them as Spring beans. FYI, all the@Controller classes which we write are discovered by this annotation.
- @Configuration designates the class as a configuration class for Java configuration. This is particularly important if you favor Java-based configuration over XML configuration.
See you in the Part 4 of Spring Boot Tutorial.
If you enjoy our content and want to support us, please consider donating via gpay, phonepay or paytm on +91 9920600280. Also, I’d appreciate if you’d buy me a coffee☕
Keep learning and growing!
-K Himaanshu Shuklaa..
No comments:
Post a Comment