HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode

Spring Boot Tutorial

Spring Boot is a Spring framework module which provides RAD (Rapid Application Development) feature to the Spring framework. It is highly dependent on the starter templates feature which is very powerful and works flawlessly.

Spring boot modules
Spring boot modules

1. What is starter template?

Spring Boot starters are templates that contain a collection of all the relevant transitive dependencies that are needed to start a particular functionality. For example, If you want to create a Spring WebMVC application then in a traditional setup, you would have included all required dependencies yourself. It leaves the chances of version conflict which ultimately result in more runtime exceptions.

With Spring boot, to create MVC application all you need to import is spring-boot-starter-web dependency.

<!-- Parent pom is mandatory to control versions of child dependencies -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath />
</parent>

<!-- Spring web brings all required dependencies to build web application. -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Above spring-boot-starter-web dependency, internally imports all given dependencies and add to your project. Notice how some dependencies are direct, and some dependencies further refer to other starter templates which transitively downloads more dependencies.

Also, notice that you do not need to provide version information into child dependencies. All versions are resolved in relation to version of parent starter (in our example it’s 2.0.4.RELEASE).

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-json</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
	</dependency>
	<dependency>
		<groupId>org.hibernate.validator</groupId>
		<artifactId>hibernate-validator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
	</dependency>
</dependencies>

Read More : Spring boot starter templates list

2. Spring boot autoconfiguration

Autoconfiguration is enabled with @EnableAutoConfiguration annotation. Spring boot auto configuration scans the classpath, finds the libraries in the classpath and then attempt to guess the best configuration for them, and finally configure all such beans.

Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration.

Auto-configuration is always applied after user-defined beans have been registered.

Spring boot auto-configuration logic is implemented in spring-boot-autoconfigure.jar. Yoy can verify the list of packages here.

Spring boot autoconfiguration packages
Spring boot autoconfiguration packages

For example, look at auto-configuration for Spring AOP. It does the followings-

  1. Scan classpath to see if EnableAspectJAutoProxy, Aspect, Advice and AnnotatedElement classes are present.
  2. If classes are not present, no autoconfiguration will be made for Spring AOP.
  3. If classes are found then AOP is configured with Java config annotation @EnableAspectJAutoProxy.
  4. It checks for property spring.aop which value can be true or false.
  5. Based on the value of property, proxyTargetClass attribute is set.
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
		AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration 
{

	@Configuration
	@EnableAspectJAutoProxy(proxyTargetClass = false)
	@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
	public static class JdkDynamicAutoProxyConfiguration {

	}

	@Configuration
	@EnableAspectJAutoProxy(proxyTargetClass = true)
	@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
	public static class CglibAutoProxyConfiguration {

	}

}

3. Embedded server

Spring boot applications always include tomcat as embedded server dependency. It means you can run the Spring boot applications from the command prompt without needling complex server infrastructure.

You can exclude tomcat and include any other embedded server if you want. Or you can make exclude server environment altogether. It’s all configuration based.

For example, below configuration exclude tomcat and include jetty as embedded server.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

4. Bootstrap the application

To run the application, we need to use @SpringBootApplication annotation. Behind the scenes, that’s equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan together.

It enables the scanning of config classes, files and load them into spring context. In below example, execution start with main() method. It start loading all the config files, configure them and bootstrap the application based on application properties in application.properties file in /resources folder.

@SpringBootApplication
public class MyApplication 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(Application.class, args);
    }
}
### Server port #########
server.port=8080
 
### Context root ########
server.contextPath=/home

To execute the application, you can run the main() method from IDE such eclipse, or you can build the jar file and execute from command prompt.

$ java -jar spring-boot-demo.jar

5. Advantages of Spring boot

  • Spring boot helps in resolving dependency conflict. It identifies required dependencies and import them for you.
  • It has information of compatible version for all dependencies. It minimizes the runtime classloader issues.
  • It’s “opinionated defaults configuration” approach helps you in configuring most important pieces behind the scene. Override them only when you need. Otherwise everything just works, perfectly. It helps in avoiding boilerplate code, annotations and XML configurations.
  • It provides embedded HTTP server Tomcat so that you can develop and test quickly.
  • It has excellent integration with IDEs like eclipse and intelliJ idea.

6. Spring Boot Configuration

  1. Spring boot annotations
  2. A Guide to Logging in Spring Boot
  3. Spring Boot – Spring Boot starter templates
  4. Spring Boot – Starter Parent Dependency
  5. Spring Boot – Get all loaded beans
  6. Spring Boot – @SpringBootApplication and Auto Configuration
  7. Spring Boot – Change Application Root
  8. Spring Boot – Configure Jetty Server
  9. Spring Boot – Tomcat Default Port
  10. Spring Boot – WAR Packaging Example
  11. Spring Boot – Logging yml Config
  12. Spring Boot – Logging property Config
  13. Spring Boot – SSL [https]
  14. Spring Boot – CommandLineRunner
  15. Spring Boot – Developer Tools Module Tutorial
  16. Spring Boot – Inject Application Arguments in @Bean and @Compoment
  17. Spring boot embedded server logs
  18. Spring boot embedded tomcat configuration

7. Developing REST APIs and SOAP Webservices

  1. Spring Boot – REST APIs
  2. Spring Boot – Jersey
  3. Spring Boot – Spring HATEOAS Example
  4. Spring Boot – Request validation of REST APIs
  5. Spring Boot – Role Based Security
  6. Spring Boot – SOAP Webservice
  7. Spring Boot – SOAP Client
  8. Spring boot 2 and ehcache 3 example

8. Other Useful Topics

  1. Disable banner at startup
  2. Spring Boot – JSP View
  3. Spring Boot – Custom PropertyEditor
  4. Spring Boot – @EnableScheduling
  5. Spring Boot – JMSTemplate
  6. Spring Boot – RSS / ATOM Feed
  7. Spring boot read file from resources folder
  8. Spring Boot Interview Questions

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
Comments are closed on this article!

Search Tutorials

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)