fbpx
Select Page

Spring Boot Framework

Spring Boot

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

Spring Boot has become a popular tool in a developer’s arsenal, especially of late. This is due to the fact that in this age of instant noodles and fast food, a similar expectation is made in application development as well – that we get through our application development quickly and businesses can start utilizing the solutions earlier.

This requires a simpler and quicker application deployment runbook as well, so that we can avoid the complex setup of application pre-requisites and management of dependencies.

Also, from an operational perspective, it’s expected to have an easier way to get a view of the application’s health status, its metrics and configuration. The other task of application dependency patching needs simplifications so as to achieve minimal business impact.

Spring Boot provides us features and solutions in all these areas, to help us achieve the primary objective of easily and quickly developed, production ready application.

In this blog, I will give you a view of how this objective is achieved via Spring Boot.

We will understand the following aspects of Spring Boot:

  • Spring Boot and how it helps in application development
  • Spring Boot Features
  • Benefits over and above core Spring framework

So, let’s get started.

What is Spring Boot?

Let’s start with the official definition from the Spring Boot website:

“Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run.”

While the definition provides a good high-level statement of the intentions of spring boot that it allows for easier creation of runnable, standalone and production ready applications; the way it achieves this intention is detailed below:

Opinionated View of Libraries

Spring Boot makes certain configuration decisions by itself, especially with respect to default configurations. This makes sense as most applications would require the same set of configurations to start with. In case a particular application requires something different, it can be easily be configured to be wired differently. The idea is that these exception scenarios where defaults don’t work for the application are minority scenarios and would be relatively rarer cases.

The principle here is that “Majority scenario configurations are defaults and provide configuration handles for minority scenarios”.

For e.g. if we need a web application, we need a container to run it in. So tomcat server is embedded by default in the application and auto configured to run as standalone application. The entire set of library dependencies and spring dependencies are added in automatically.

Auto-Configuration

Spring Boot automatically configures the components and framework wirings based on the application selected and the dependencies added. It builds a few typical patterns and also based on the patterns and the jar files available, auto-configures a few typical aspects of the spring application.

For example, if we are building a web application, spring boot would configure views and view resolvers automatically.

This reduces the time a developer would take to set up the application and instead they can focus on the core application logic.

Version Management

Version Management of libraries, that the application depends on, turns out to be a big task once the application is developed and deployed on production. We need to update some version of the libraries periodically for various reasons – need to use a feature in recent library version, security/bug fixes in a library, out-of-support library version forcing an update, etc. These bring up the issue of updating the libraries without breaking the application. Interdependent libraries usually have a compatible dependent library version that it works best with. Spring Boot takes over this responsibility of managing version of the dependent libraries.

You just get the latest or correct version of the spring boot, and the corresponding compatible library versions are available to the application.

Spring Boot starter provide one such easy way as an example.

Spring Boot Tooling

Spring Boot provides multiple tools to aid faster application development. These include Spring Boot CLI, Spring Initializr and Spring Boot IDEs like Spring STS Suite, Eclipse and IntelliJ IDE, etc.

Thus the primary goals of Spring Boot are:

  • Provide a really fast, easy and widely accessible way for all Spring development and provide a better experience while starting the application development process
  • Be opinionated out of the box by default, but provide an easy way to quickly change it as requirements start to diverge from the defaults
  • Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration)
  • Absolutely no code generation and move away from requirement of XML configuration

 

Challenges with Spring Based Applications

Spring Framework provides the core feature of dependency injection. In addition, it also integrates really well with other frameworks like Hibernate, JPA, Logging frameworks, etc. By itself, spring also provides frameworks of its own, for example – Spring MVC for web application development and REST service development.

So, in summary this wide integration and dependency management requires configuring the core spring framework to work as per the developer’s requirement.

And to achieve this, spring-based applications have to configure a number of beans and components within it.

For example, you would have the web.xml configuration:

<servlet>

  <servlet-name>dispatcher</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <init-param>

<param-name>contextConfigLocation</param-name>

<param-value>WEB-INF/ServiceContext.xml</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

 

You might also have the spring context configuration as:

<mvc:annotation-driven></mvc:annotation-driven>

<context:component-scan base-package=”com.example”></context:component-scan>

 

Also, you would need to configure the transaction manager as well:

<tx:annotation-driven transaction-manager=”txManager“/>

<bean id=”txManagerclass=”org.springframework.orm.hibernate5.HibernateTransactionManager“>

<property name=”sessionFactory“>

<ref bean=”mysessionFactory“/>

</property>

</bean>

 

As you would have got the hint from above, there is a number of boilerplate configuration and code that needs to be completed before we get to the juicy part of developing our application logic.

And, consider the part of wiring in the cross-cutting aspects like logging libraries, monitoring features, etc. And the need to do the exact same thing for every project you develop in spring increases and so does the irritation of the necessity of the repetition.

The story is the same for any spring-based application.

Spring Boot was created to target these challenges in Spring Application development.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

Spring Boot Internals

Spring Boot Internals

Introduction to Spring Boot Features

Spring Boot Starter

Spring Boot provides a major feature called Starter projects. The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies that are typical to an application being developed into single dependencies. This provides these key benefits:

  • Simplifies the dependency that the developer needs to manage himself
  • The dependency that the developer needs to manage is now directly related to the application type
  • The task of managing the version of the dependencies is now the task of the starter dependency the developer has added to the project

 

For example, let’s consider that we want to develop a Spring WebApplication with Tomcat WebServer. Then we need to the following jar dependencies at the minimum in web application project

  • Spring core dependency (spring-core-<xxx version>.jar)
  • Spring Web dependency (spring-web-<xxx version>.jar)
  • Spring Web MVC dependency (spring-webmvc-<xxx version>.jar)
  • Servlet dependency (servlet-<xxx version>.jar)

If it’s an application with database integration, then we need the database related jar dependencies like Spring JDBC jar file, Spring ORM jar files,Spring Transaction Jar file etc.

  • Spring JDBC dependency (spring-jdbc-<xxx version>.jar)
  • Spring ORM dependency (spring-orm-<xxx version>.jar)
  • Spring Transaction dependency (spring-transaction-<xxx version>.jar)

We need to define all these dependencies in our build files. It is very tedious tasks for a Developer and difficult to manage in the future. Consider the fact that we might need to upgrade the framework in the future – a lot of dependencies and jar versions would need to change. And also, it increases our build file size.

Spring Boot Starter takes away this complexity. It combines all related jars into single jar file, typical for an application scenario like a web application or web application with database. So, we can add only one jar file dependency to our build files.

So, in the example above we just need “spring-boot-starter-web” dependency in our build file.

When we add “spring-boot-starter-web” jar file dependency to our build file, then Spring Boot Framework automatically downloads all required jars and adds them to our project classpath.

 

Some of the important and commonly used starter projects are:

  • spring-boot-starter-web-services

Starter project to quickly build SOAP Web Services

  • spring-boot-starter-web

This is to build RESTful web applications, using Spring MVC. Tomcat is configured as the default embedded container

  • spring-boot-starter-hateoas

Build hypermedia-based RESTful web applications, using Spring MVC and Spring HATEOAS

  • spring-boot-starter-jersey

This alternative to string-boot-starter-web is used for building RESTful web applications using JAX-RS and Jersey library

  • spring-boot-starter-security

Starter project when using Spring Security

  • spring-boot-starter-jpa

This is useful for applications that need Spring data JPA with hibernate

  • spring-boot-starter-data-rest

This starter is used for applications that expose Spring Data repositories over REST and use Spring Data REST

Spring Boot AutoConfiguration

Spring Boot Auto Configuration looks to bring more intelligence into application configuration. The idea is to reduce manual spring configuration and provide spring framework the capability to reduce these configurations automatically.

For example, once the starter dependencies are available, it builds a few typical patterns and also based on the patterns and the jar files available, auto-configures a few typical aspects of the spring application.

The objective of Spring Boot Application is that we don’t define single XML configuration and almost none/minimal Annotation configuration. Spring Boot AutoConfigurator component is tasked with providing the relevant information to spring framework.

For instance, if we were to build a web application with Spring MVC, then we need to define lot of configuration like views, view resolvers etc. But if we use Spring Boot Framework, spring boot autoconfiguration would configure views and view resolves automatically.

We could use “spring-boot-starter-web” dependency and have the jar file in our project build file (or maven pom), then Spring Boot AutoConfigurator will resolve views, view resolvers etc. automatically.

Spring Boot also reduces Annotation configuration.

For example, if we use @SpringBootApplication annotation at class level, then Spring Boot AutoConfigurator will automatically add all required annotations to Java Class ByteCode.

That is, @SpringBootApplication annotation will add @Configuration, @ComponentScan and @EnableAutoConfiguration annotations to the Java Class bytecode automatically.

This reduces the time a developer would take to set up the application and instead they can focus on the core application logic.

Spring Boot Frameworks Integration

Spring Boot supports integration and configuration of a number of frameworks. Some of them are:

  • Logging
  • Transaction management
  • Error/Exception handling
  • Monitoring and health checks
  • Integrating unit testing and mocking frameworks

We just need to add in the framework to the build and spring boot autoconfigures them as per its defaults.

Spring Boot Version Management

Spring Boot takes care of the management of version of the dependent libraries needed by the application.

As a developer, we just need to migrate spring boot (starter) to the required version/latest version, and the compatible versions are automatically imported and available into the application.

Spring Boot Actuator

Spring Boot Actuator provides two major features that are very useful to the application team:

  • Provides Management EndPoints to Spring Boot Applications

This is useful to allow the developer/application team to manage the application at runtime.

The endpoints provided include ones for health status (/health), application information (/info), application metrics (/metrics), application auditevents (/auditevents) and many others. Some of them are available by default while others can be enabled by the developer as per the needs.

  • Spring Boot Applications Metrics.

The actuator provides a way to manage and monitor the application metrics like CPU usage, etc.

On running the Spring Boot Web Application using CLI, Spring Boot Actuator automatically provides an application management endpoint at “http://localhost:8080/”.

We can use HTTP GET/POST Request methods for the management endpoint to get the application metrics and configuration or manage and manipulate them.

Spring Boot CLI

Spring Boot Command Line Interface (CLI) is a Spring Boot tool to run and test Spring Boot applications from command prompt.

It lets you run groovy scripts. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfiguration components to resolve all dependencies and execute the application.

We can run even Spring Web Applications with simple Spring Boot CLI Commands.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

Spring Tools

Spring Boot provides further tools that allow better development experience.

These include:

  • Spring Initializr

Spring Initializr is a tool that helps in generation of JVM-based projects with implementations for:

  • Java, Kotlin, Groovy languages
  • Maven and Gradle build tools
  • Hook points for custom resource generation

Spring Initializr can be used from command line using Spring Boot CLI, from IDE that has the Spring plugin/extension or a Web UI (spring provides its own webui are https://start.spring.io )

  • Spring IDEs

Spring IDEs allow easy Spring application development by providing a way to quickly develop, debug, test, version manage, build and deploy spring and spring boot applications.

Spring STS Suite is a configured IDE that supports all Spring framework development. Also Spring Boot plugins are available for very popular Eclipse and IntelliJ IDEs. Plugins/extensions for other IDEs used by the developer community are also available.

In Conclusion

I hope this blog would have helped you understand Spring Boot and also understand where it fits and helps in your application development requirements and process. Hopefully you would also have understood the key features of Spring Boot and the concepts around it. In the next blog we shall delve further into exploring these features as we develop a sample spring boot application.

Looking forward to your experiences and comments here.

Author: Kiran Tiwari

Designation: Software Trainer

Company:  Seven Mentor Pvt. Ltd.

kirru

 

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.






Pin It on Pinterest