Introduction
Spring Boot is an open-source framework in Java introduced by Rod Johnson in 2013. It was introduced to simplify the development of standalone, production-grade, and Spring-based applications. This framework minimizes the efforts of developers in configuration while building applications and focuses on writing the logical algorithms that meet the business requirements.
Advantages of Spring Boot Framework
Given below is the list of advantages of using the Spring Boot framework
1. Auto-configuration: Spring Boot framework auto-configures many aspects of your applications based on dependencies present in your project, and hence reduces the need for manual configuration.
2. Embedded Servers: The Spring Boot framework comes with built-in servers like Tomcat, Jetty, or Undertow, eliminating the need for an external server for deploying the applications. For example, if you start your Spring Boot project with the Spring Web Starter, it automatically pulls your application to spring-boot-starter-tomcat as the default embedded server.
3. Opinionated starters: In Spring Boot, a starter is a pre-packaged set of dependencies that makes it easy to add specific functionality to your application. Opinionated starters mean Spring Boot by default chooses the most common libraries for you while developing the applications, instead of asking you to choose the libraries from the dozens of options available. You can always override the defaults set up if needed.
4. Reduced boilerplate: Spring Boot minimizes the use of extensive XML configuration or manual setup, and hence, it simplifies the development of applications for developers.
Installation Prerequisites
Before starting with the Spring Boot framework, make sure to install the list of software below in your system
1) Java JDK: The Java Development Kit(JDK) is a software development kit that provides tools and other resources necessary for developing Java applications. It provides the Java Runtime Environment to run the code and see the result. The version of Java JDK should be 17 or 21. It includes the Java Virtual Machine, which executes the Java bytecode, which is a compiled form of Java source code. The Java Virtual Machine acts like an interpreter, translating the platform-independent bytecode into machine code so that the hardware can understand and execute the code.
2) Maven or Gradle: Both Maven and Gradle are popular tools for building applications using the Spring Boot framework. The features of both Maven and Gradle are listed below
Maven:
1. Convention Over Configuration: Maven provides you with pre-defined libraries and prioritizes standard project structure, which can simplify setup. If you follow predefined conventions, then you don’t need to write extra configuration. Maven applies the principle of convention over configuration heavily, so developers can build projects with minimal configuration
2. XML-based Configuration: Maven uses XML for its Project Object Model(POM) files to manage and configure your project. This file tells Maven how to build and manage dependencies for your applications. Maven supports XML-based configuration through its pom.xml, where you can define project metadata, declare dependencies, configure plugins, and set up build profiles.
3. Stability and Predictability: Maven has a vast array of plugins for various tasks, offering robust support for common development needs.
4. Strong plugin ecosystem: Maven’s convention-driven approach often leads to more consistent builds, especially in CI/CD environments.
Gradle:
1. Flexibility and expressiveness: Gradle uses a Groovy or Kotlin DSL(Domain Specific Language) for its build scripts, offering more flexibility and programmatic control over the build process.
2. Performance: Gradle often boasts faster build times, particularly for incremental builds, due to its efficient caching mechanism and parallel task execution.
3. Customization: The DSL allows for highly customization build logic, making it suitable for complex projects or unique build requirements.
4. Modern Features: Gradle supports features like incremental builds, build caching, and build scans, which can improve developer productivity.
An IDE
IDE stands for Integrated Development Environment, suitable for Spring Boot Development, each different IDE offering distinct advantages. The following is the list of IDEs you can use for developing applications using Spring Boot
1. IntelliJ IDEA: This is a highly recommended and popular choice for Spring Boot. Its latest version provides extensive built-in support for Spring Framework. It offers features like direct project creation, refactoring tools, and robust debugging capabilities.
2. Visual Studio Code: VS Code is a lightweight and highly extensible code editor. It can also be used for Spring Boot development, including Spring Boot external packages, extensions. It provides a rich ecosystem provides excellent support for Spring Boot projects.
3. Eclipse: A widely used IDE for Spring Boot development, which provides specialized spring development features in the standard Eclipse environment.
4. Apache NetBeans: This is another open-source IDE for Java Development. NetBeans also supports Spring Boot development through the NB SpringBoot plugin, offering features for project creation and management.
The choice of an IDE often depends on individual preferences of developers, project requirements, and familiarity with the environment. IntelliJ IDEA and STS are often favored for their comprehensive Spring-specific features, while VS Code is preferred for its lightweight nature and extensibility.
Your First Spring Boot Application
Given below is the Java program for set up the environment for developing application
// Java program for an entry point in spring boot
@SpringBootApplication
public class MyFirstApplicaion {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Explanation: The above program will not print any output, but it will set up everything required for running your application.
• @SpringBootApplication: This is a special annotation that combines three important annotations, which are @Configuration for making the declared class a configuration class, @EnableAutoConfiguration for making the Spring Boot automatically configure beans and dependencies, and @ComponentScan for scanning the package for components (like controllers, services, repositories).
• public static void main(String[] args): This is a standard program line in Java for declaring the main method from where the execution starts.
• SpringApplication.run(DemoApplication.class, args): This line will boot up the Spring application and start running your application on an embedded server of Spring Boot(like Tomcat, Jetty, and Undertow).
REST Controller
// Java program to print the message on springboot application
@RestController
public class printMessage {
@GetMapping(“/Welcome”)
public String Welcome() {
return “Welcome to Spring Boot”;
}
}
Explanation: The above program will print the output on the embedded server, and it will display the message (Welcome to Spring Boot) on the localhost server.
• @RestController: This line will tells Spring Boot that this class is a REST Controller. It combines @Controller and @ResponseBody for web controllers and sends return values directly as HTTP responses.
• GetMapping(“/Welcome”): This line map the HTTP GET requests to the /Welcome URL and execute the code on the browser localhost.
• public String Welcome(): This method will handle the request, and it will return the string “Welcome to Spring Boot”. This method is a REST controller, so the string declared will become the HTTP response body.
Configuration
Spring Boot uses external configuration, which means you can modify your app without changing the code. This can be achieved by
1. Configuration Files
• application.properties(it is key-value format).
• application.yml(YAML format is more structured)
Examples
// examples for application.properties
server.port=9090
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.database.username=tpointtech
spring.datasource.password=2345
// example for application.yml
server:
port: 9090
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: tpointtech
password: 2345
2. Profiles (Different Environment): For creating separate configuration files for different environments.
Examples
application-dev.properties // runs Database on localhost
application-prod.properties // runs Database on cloud server.
Connecting to Database
Connecting a Spring Boot application to a MySQL database involves several steps:
1. Add MySQL Connector Dependency: add spring-boot-starter-data-jpa and the MySQL JDBC driver(mysql-connector-java) to your project. Spring Boot will auto-configure a Database if it finds a JDBC driver on the classpath and properties defined above.
2. Auto Configure Database Properties: Spring boot detects the data source(spring.datasource.*) and creates a DataSource bean, configures EntityManagerFactory for JPA and the PlatformTransactionManager.
3. Connection Pool: The DataSource bean keeps a pool connected.
4. Hibernate bootstrap: Hibernate, which is a Java framework, reads spring.jpa.* setting and maps your classes to DB tables.
5. Runtime: Your repositories obtain a connection from the pool to run queries.
Project Ideas to build using Spring Boot
Given below is a list of project ideas that you can build using Spring Boot framework:
1) Spring Boot Actuator
In this project, you will build a software application that will monitor and give the status of your health. While building this project, you will learn to build a scalable application that gives an accurate reading and learn how to write APIs for building scalable applications.
2) Spring Security(authentication and authorization)
In this project, you will build a security application that allows only valid users to use the system or website and blocks the invalid users from performing any kind of activity on the app or website. This project will make your understanding of security fundamentals strong and prepare you for industry-relevant skills.
3) Easy Database interaction
In this project, you will build a database that will store the information of the user, and its user interface should be easy to use, fast to load, and less prone to error. The benefit of building this project is that your core concept of a database will be crystal clear. You will learn to include different Spring Boot modules in your project, like JPA(Java Persistence API) and Hibernate. You will learn to handle Database Configuration from setting up properties to connecting pooling basics.
4) Deploying Spring Boot Application
In this project, you will deploy your application or project on the internet. You can deploy your applications of Spring Boot applications on JAR, Docker, or the Cloud. This is a valuable project because it will show you the complete journey from development to packaging and from packaging to deployment. This project will give you an understanding of real-world deployment workflows used in companies, which makes you job-ready because deployment is a must-have skill for backend developers.
Conclusion
This article gives a description of Spring Boot, which is a Java framework used for building scalable applications with working examples and along with their explanation.
I hope this article has provided you a valuable information. If you are looking for more such kind of articles, I suggest you visit the Tpoint Tech Website, where you can find various articles, on programming with interview questions, working examples , and an online compiler where you can run your code.