What is Spring?
The spring framework is one of the most popular application development frameworks for java. one of the best features in spring is that it has the dependency injection (di) or inversion of control (ioc), which allows us to develop loosely coupled applications. and, loosely coupled applications can be easily unit-tested.
Spring is an open-source lightweight framework widely used to develop enterprise applications.
It’s packed with some nice features like Dependency Injection, and out of the box modules like: Spring JDBC , Spring MVC ,Spring Security ,Spring AOP ,Spring ORM ,Spring Test .
These modules can drastically reduce the development time of an application.
For example, in the early days of Java web development, we needed to write a lot of boilerplate code to insert a record into a data source. By using the JDBCTemplate of the Spring JDBC module, we can reduce it to a few lines of code with only a few configurations.
What is SpringBoot?
Spring Boot is an open-source Java-based framework used to create a micro Service. It is developed by the Pivotal Team and is used to build stand-alone and production-ready web based spring applications.
Spring Boot is built on top of the conventional spring framework, widely used to develop REST APIs.
Micro Service is an architecture that allows the developers to develop and deploy services independently. Each service running has its own process and this achieves the lightweight model to support business applications.
Spring Boot provides a good platform for Java developers to develop a stand-alone and production-grade spring application that you can just run. You can get started with minimum configurations without the need for an entire Spring configuration setup.
Spring Boot is basically an extension of the Spring framework, which eliminates the boilerplate configurations required for setting up a Spring application.
Here are just a few of the features in Spring Boot:
Opinionated ‘starter’ dependencies to simplify the build and application configuration
Embedded server to avoid complexity in application deployment
Spring Boot is designed with the following goals −
To avoid complex XML configuration in Spring
To develop a production ready Spring applications in an easier way
To reduce the development time and run the application independently.
Spring Boot is the combination of Spring Framework and Embedded Servers.
Why SpringBoot?
· It provides a flexible way to configure Java Beans, XML configurations, and Database Transactions.
· It provides a powerful batch processing and manages REST endpoints.
· In Spring Boot, everything is auto configured; no manual configurations are needed.
· It offers annotation-based spring application
· Eases dependency management.
· It includes Embedded Servlet Container.
spring boot does all of those using autoconfiguration and will take care of all the internal dependencies that your application needs — all you need to do is run your application.
How does it work?
Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation.
Auto Configuration
Spring Boot Auto Configuration automatically configures your Spring application based on the JAR dependencies you added in the project. For example, if MYSQL database is on your class path, but you have not configured any database connection, then Spring Boot auto configures an in-memory database.For this purpose, you need to add @EnableAutoConfiguration annotation or @SpringBootApplication annotation to your main class file. Then, your Spring Boot application will be automatically configured.
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
public 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 contains @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 @SpringBootApplication annotation to the class, you do not need to add the @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration annotation. The @SpringBootApplication annotation includes all other annotations.
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
ComponentScan
With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.
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 your components added in your project.
with Spring Boot is that many things happen implicitly. We use the @SpringBootApplication annotation, but it’s a combination of three annotations: @Configuration,@EnableAutoConfiguration,@ComponentScan
import org.springframework.boot.SpringApplication;import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
Spring Boot Starters
Before Spring Boot was introduced, Spring Developers used to spend a lot of time on Dependency management. Spring Boot Starters were introduced to solve this problem so that the developers can spend more time on actual code than Dependencies. Spring Boot Starters are dependency descriptors that can be added under the <dependencies> section in pom.xml. There are around 50+ Spring Boot Starters for different Spring and related technologies. These starters give all the dependencies under a single name. For example, if you want to use Spring Data JPA for database access, you can include spring-boot-starter-data-jpa dependency.
The advantages of using Starters are as follows:
· Increase productivity by decreasing the Configuration time for developers.
· Managing the POM is easier since the number of dependencies to be added is decreased.
· Tested, Production-ready, and supported dependency configurations.
· No need to remember the name and version of the dependencies.
Spring Boot Starter Data JPA is illustrated below:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
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 a 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>
Conclusion
The most important feature of the Spring Framework is dependency injection wheras the most important feature of the Spring Boot is Autoconfiguration.
Developers need to write boilerplate code for smaller tasks whereas In Spring Boot, there is reduction in boilerplate code.
To create a Spring application, the developers write lots of code wheras in springboot it reduces the lines of code.
Comments