Getting Started with Spring Boot
What is Spring Boot?
Spring Boot is now an open-source Java-Based framework to create standalone microservices with production-ready features. It is heavily maintained by the Pivotal team. Microservices is an architectural design that creates scalable, loosely coupled, and testable applications with a single function module with a well-defined interface.
What is MicroService?
MicroService is an architecture that allows developers to develop and deploy services independently. Each service running has its own process, and this achieves the lightweight model to support business applications.
Build tools — Maven and Gradle
For building heavy projects, there is a need to sagreting code into components so that they can be reused again within the same project or by other projects.
The components are stored in a location called a repository. These components can be referred to as artifacts when we talk about any build tool.
- Maven is the project management build tool developed by Apache Org, which adds the functionalities of Java libraries through dependencies. Maven is a staged-driven build tool, and its lifecycle is divided into stages: validate, compile, test, package, verify, install and deploy. The core component of the Maven project is pom.xml.
- Gradle is an open-source build tool that adds functionalities of java libraries through plugins. The lifecycle of the Gradle build is divided into phases, such as initialization, configuration, and execution. The core component of the Gradle project is the build.gradle file.
How does it work? @SpringBootApplication
Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation.
For example,
If MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.
The entry point of the spring boot application is the class containing @SpringBootApplication annotation and the main method.
Spring Boot automatically scans all the components included in the project by using @ComponentScan annotation.
@SpringBootApplication = @EnableAutoConfiguration + @ComponentScan + @SpringBootConfiguration
Spring Boot starters
Spring Boot provides a number starter pack that can be used while developing an application. These starter packs, when loaded into the classpath, get into action.
The spring Boot framework follows the spring-boot-starter-* pattern for all its starter packs, where “*” can be replaced with the utility provided.
Examples
Look at the following Spring Boot starters explained below for a better understanding −
- Spring Boot Starter Actuator dependency is used to monitor and manage your application.
Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- Spring Boot Starter Security dependency is used for Spring Security.
Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- Spring Boot Starter web dependency is used to write the Rest Endpoints.
Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- Spring Boot Starter Thyme Leaf dependency is used to create a web application.
Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- Spring Boot Starter Test dependency is used for writing Test cases.
Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test<artifactId>
</dependency>
Auto Configuration
Spring Boot Auto Configuration automatically configures your Spring application based on the JAR dependencies you added to the project. For example, if the MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.
For this purpose,
you need to add the @EnableAutoConfiguration annotation or @SpringBootApplication annotation to your main class file.
Then, your Spring Boot application will be automatically configured.
Observe the following code for a better understanding −
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfigurationpublic
class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Spring Boot Application
The entry point of the Spring Boot Application is the class containing @SpringBootApplication annotation. This class should have the main method to run the Spring Boot application. @SpringBootApplication annotation includes Auto- Configuration, Component Scan, and Spring Boot Configuration.
If you added the @SpringBootApplication annotation to the class, you do not need to add the @EnableAutoConfiguration, @ComponentScan, and @SpringBootConfiguration annotations. The @SpringBootApplication annotation includes all other annotations.
Observe the following code for a better understanding −
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic
class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Core Spring framework annotations
Bean
@Bean is an annotation that is used on the top of a method and can act as a replacement for the XML </bean> element. to declare a bean, simply annotate the method with @Bean annotation.
import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration class DemoApplication { @Bean public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }} }
Autowired
@autowired is used to make a constructor, field, or setter method to get autowired by Spring DI. Dependency Injection is a technique that removes the dependency of a component from the source code so that our code is loosely coupled with the component.
import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan;@ComponentScanpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Component Scan
Spring Boot application scans all the beans and package declarations when the application initializes. You need to add the @ComponentScan annotation for your class file to scan the components added to your project.
Observe the following code for a better understanding −
import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan;@ComponentScanpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
References
tutorialspoint, javatpoint, docs.spring.io, baeldung;