For every Java developer Hibernate, Spring are everyday terms. Although recently there was an addition to this list of ‘Spring-boot’. Nowadays Spring Boot is one of the most used open source frameworks. Hence if you are a java developer you must know about spring boot. In this tutorial we will cover srping boot features in detials.
Spring-boot can be used to create stand-alone and web enterprise applications with minimal effort. The most important part of this framework is that it makes development very convenient by removing lengthy, repetitive boilerplate code and common configuration steps.
Is it worth learning Spring Boot?
Yes, of course. Java is one of the most popular and most used programming language. Java with spring is the most powerful combination. After releasing spring boot it has got even more popularity. Spring boot has made things very easy. So learning spring boot will definitely help.
What is basic requirement to setup a spring boot project?
System Requirements for spring boot project setup are
|Java||Java 8 or above|
|Spring Framework||Spring Framework 5.2.8.RELEASE or above|
|Maven Or Gradle||Maven 3.3 + Or gradle 6.3+|
Things that make Spring boot Awesome!
Reasons why spring boot is most used:
- Quick and easy Project setup
- Production ready Spring applications ready in few and easy steps
- Easy to understand and develop application
- Increases productivity-reduce the development time for redundant code
- Auto configuration is possible
- Dependency management handled internally
- Provide opinionated ‘starter’ dependencies to simplify your build configuration
- Auto dependency management using Spring Boot starters
- Application Entry point denoted by @SpringBootApplication
- Auto configurations
- Component Scan
- Externalized Configuration
- Out of the box spring data jpa support
1. Auto dependency management using Spring Boot starters:
Any project, even a standalone project, is dependent on some libraries for something. These libraries are available as dependencies/jars. Sometimes it happens that even these libraries are dependent on some other libraries. Most of the times they need specific versions. If the related dependency is missing or versions are not compatible the application does not work as expected.
We cannot deny that managing the dependencies is difficult. Spring boot solves this problem by providing dependency-management section.
Spring boot provides starter packages which includes jars and the required versions, so we do not need this anymore 🙂 . It configures the dependency versions based on the release. If we need specific version we can off course specify the version for dependency, However it is not recommended.
Point to remember is that each version of spring boot has different versions. If we upgrade the Spring Boot version, related dependencies will be upgraded automatically.
All Spring Boot starters are named as spring-boot-starter-[starter-name].
eg: spring-boot-starter-web or spring-boot-starter-data-jpa
You can visit starters list to see all available spring boot startes.
2. Application Entry point denoted by @SpringBootApplication:
For project, as it grows, it is difficult to find out the entry point of the project after some times. As a spring boot application is a java application the entry point is Main method. When we give control to string boot by calling SpringApplication.run method, this main method is found and executed. To run() method we need to pass class name which is the primary component and args array. Primary component is the class with @SpringBootApplication annotation.
Note that @SpringBootApplication annotation includes @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration annotations. That means if we add @SpringBootApplication annotation these annotations will be added automatically.
We can run spring boot application in different ways.
- By using IDE as java application
- Using building tool like maven as mvn spring-boot:run
We should see the starter banner once the application starts. We can also customize spring-boot start up banner.
3. Auto configuration:
One more problem that every developer has faced while developing a project is that of extensive configurations. Configurations for database, flyway, rest API, security etc. are required in any and all applications. These can be sometimes excessive and time consuming.
But spring-boot resolves this situation too. Spring Boot does the configurations automatically according to the dependencies added. For instance, if we add spring-boot-starter-web dependency to our project, then webserver, servlets etc. will be configured automatically.
We must use @EnableAutoConfiguration annotation to infrom srping boot that auto configuration is requried.
Note: As we read earlier, including @SpringBootApplication annotation will include @EnableAutoConfiguration annotation.
4. Component scan:
One of the most important feature of spring is dependency injection. To detect the classes for auto injection, spring uses special stereotype annotations such as @Component, @Controller, @Service, and @Repository.
In order to create objects of these annotated classes and inject them, spring needs to know where they are located. One way to do that is by using @ComponentScan annotation.
In spring boot application @SpringBootApplication annotation adds @ComponentScan annotation with default setting. Hence by default spring boot scans the annotated classes under the current package. Due to this it is recommended that we put our main class in the root or top most package.
We can also override @ComponentScan annotation to specify packages to scan. If we need to scan paths other than the root package. There are many way to do that, some ways are as below,
For more details visit: ComponentScan java doc
5. Externalized configuration
Frequently there are scenarios where some data used in an application is different for different environments. Such as, the port number, where the application is deployed is different for development and production environment. In such cases, hardcoding them in the code could be difficult to manage and change.
Spring boot allows multiple options to externalize this properties. We can use these configured properties using the @Value annotation or by binding properties to objects using @ConfigurationProperties.
Configuration properties can be provided using configuraion files,, command line or system setting.
File configuration can be provided by adding files like application.properties, application.yaml or in the classpath. In general src/main/resources directory is used to store configuration files.
application.properties file example
application.yaml file example
We can also change the configuration file name by using spring.config.name property.
Config file location can be changed using spring.config.location environment property.
6. Out of the box spring data jpa support:
Spring data jpa is one of the most important support provided by spring boot. Spring boot provides Spring data JPA using hibernate as default JPA provider. These feature can be enabled by adding just spring-boot-starter-data-jpa dependency and by providing database source.
Spring Data JPA provides repository abstraction, that means we don’t need to worry about writing implementation of repository classes any more. Just adding meaningful method name in interface is enough.
Fast tract reading
- Spring boot is most used opensource framework by spring community
- It helps to create production ready project in easy and simple steps
- Removes repeated and complex processes
- Increases productivity-reduce the development time
- It supports auto dependency management using Spring Boot starters
- Auto configures most of the things like database, rest API, security etc
- It comes with by default support for spring data jpa