Learn Spring Boot in 30 Minutes

Learn Spring Boot

Learn Spring Boot in 30 minutes

Overview

Spring is a framework that allows you to build Spring-based and production-ready applications that you can run. Spring started out as a primarily dependency injection tool long time ago but has significantly evolved into a huge framework over time. The main benefit about Spring boot is it allows you to build or bootstrap enterprise applications that are production-ready. It is a huge and versatile framework that lets you do a lot of things easily on your way to build an application. That said, there are also a few limitations of the framework. The availability of multiple deployment approaches can be confusing and the developer can have restricted choices of building the application. The main objective of this article is to provide assistance in working with spring boot applications.

The assumption here is that a spring boot application has been developed and it is running. The sections below provides some guidance on management of a spring boot application. Another assumption is that a database that has an in-memory data structure store and can be used as a message broker and a cache.








Must watch -Spring video tutorials 

Video Tutorials – Spring Boot 

Update dependencies

Before using a spring session, update the dependencies. If you are using Maven, add the following dependencies in the pom.xml file.

<dependencies>

<!– … –>

<dependency>

<groupId>org.springframework.session</groupId>

<artifactId>spring-session</artifactId>

<version>1.3.0.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-<database name></artifactId>

</dependency>

</dependencies>

Must read – Build your application using AngularJS and Spring data JPA? 








Configuring the Spring boot application

The spring configuration creates a Servlet Filter that replaces the HttpSession implementation with an implementation supported by a Spring session. To do this, add the following configuration:

@Enable<databasename>HttpSession

public class HttpSessionConfig {

}

Note that the @Enable<databasename>HttpSession is an annotation that creates a Spring Bean with the name springSessionRepositoryFilter. This filter creates the Servlet filter as mentioned earlier.

You will also like to read – Steps to learn Spring Batch Service 

Configure the database connection

After the above steps are completed, Spring Boot automatically creates a <databaseconnection>Factory which establishes a connection between the Spring session and the local host. If you are working on a production environment, you need to make sure to update the configuration to point to the database server. An example is given below:

src/main/resources/application.properties

spring.<database>.host=localhost

spring. <database>.password=secret123

spring. <database>.port=6379

Initialize server container

It has already been mentioned earlier that spring configuration, done above, creates a Servlet Filter that replaces the HttpSession implementation with an implementation supported by a Spring session. To enable the servlet filter, spring first needs to load the Config class. Lastly, this needs to be ensured that the Servlet Container (which is Tomcat) utilizes the springSessionRepositoryFilter for every request that will be processed. After this step is completed, you are ready to boot the sample Spring application.



Must read – Spring with Testing frameworks – How does it work? 

Boot sample Spring Application

You can run the sample Spring application by obtaining the source code and using the command given below:

$ ./gradlew :samples:boot:bootRun

Note that Gradle is being used as a build tool. Also, to run the sample application, you need to install the correct version of database on the local host and run it with the default port. After this is done, the sample application can be run on http://localhost:<port>/

Log in to the application

Now that you have done the necessary configuration, it is time to test the application by logging in to it. To log in, complete the following steps:

  • Enter the user name.
  • Enter the password.
  • Click the Login button.

Now you should be logged in to the application. After you have entered the login credentials such as the user name and password, the data is stored in the used database and not in Tomcat’s HttpSession implementation. So, instead of using the HttpSession implementation, the values are being persisted in the database. As mentioned earlier, the spring session replaces the HttpSession with an implementation supported by the database chosen for the purpose. After the SecurityContextPersistenceFilter of the Spring session saves the SecurityContext to the HttpSession, the values are persisted in the database.

While the above steps are given to work after a sample application with Spring boot has been developed, there are several other ways by which a sample Spring boot application can be managed. The spring framework is a massive framework and it provides numerous ways to work and develop spring boot applications.

You will also like to read – Steps to schedule tasks using Spring framework 








Sample application

We have the following 3 files to be created to run the application. First is the controller file with request mapping. The 2nd is the the POM file for configuring beans. The 3rd one is the main application to be executed to show the output.

Listing 1: Controller code

package com.techalpine.springframework;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class BootController {

@RequestMapping(“/”)

public String index() {

return “Welcome to Spring boot application!”;

}

}

Listing 2: Configuration code

<?xml version=”1.0″ encoding=”UTF-8″?>

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>org.springfrmwork</groupId>

<artifactId>gs-spring-boot-app</artifactId>

<version>2.5.6</version>

<parent>

<groupId>org.springfrmwork.boot</groupId>

<artifactId>spring-boot-strtr-parent</artifactId>

<version>1.5.3.RELEASE</version>

</parent>

<dependencies>

<dependency>

<groupId>org.springfrmwork.boot</groupId>

<artifactId>spring-boot-strtr-web</artifactId>

</dependency>

<dependency>

<groupId>org.springfrmwork.boot</groupId>

<artifactId>spring-boot-strtr-parent</artifactId>

</dependency>

</dependencies>

<properties>

<java.version>1.8</java.version>

</properties>

<build>

<plugins>

<plugin>

<groupId>org.springfrmwork.boot</groupId>

<artifactId>spring-boot-mvn-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

Listing 3: Main application program

package com.techalpine.springframework;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ApplicationContext;

@SpringBootApplication

public class BootApplication {

public static void main(String[] args) {

ApplicationContext actx = SpringApplication.run(BootApplication.class, args);

System.out.println(“Checking beans supplied by Spring Boot:”);

String[] beanNms = actx.getBeanDefinitionNames();

Arrays.sort(beanNms);

for (String beanNm : beanNms) {

System.out.println(beanNm);

}

}

}

When this main program is executed it will show all the bean names supplied by the Spring Boot framework.

Must read – Building Restful web service using Spring? 

Conclusion

While the spring boot seems a good option to develop spring options, it is not without its drawbacks. For software developers, it is difficult to follow the concepts of dependency injection and IoC. If the full value of dependency injection and IoC is not understood, then the full potential of Spring cannot be realized. The dynamic nature of Spring framework poses a huge problem. Some consider the dynamism as too fast and too unsettling. At its present state, the annotation-based Spring configuration is not something that is easy to use. Developers also are reluctant because of too much dependency because of the dependency injection feature. However, all things considered, the Spring Boot is a versatile option to bootstrap an application.

Read more Spring articles – Spring Archive

 


Please follow and like us:
0

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 7 = 10

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+
http://techalpine.com/learn-spring-boot-in-30-minutes">
LinkedIn