Home »
Articles/Editorial
Developing Applications using Spring Boot Framework
By: James Warner, on 27 JUL 2016
Summary
Technology: Spring Boot is a framework for Java language, which is implemented on top of Spring framework, it is used remove all boilerate code like writing web.xml file, creating war file and deploying war in any application/web servers like Tomcat etc...
About Spring boot Framework
Spring Boot is framework on top of all spring modules, spring boot providers starters for all different modules which will provide the auto-configuration.Spring Boot will scan the classpath of the project to bootstrap the spring bean configuration.
Eg: If the classpath contains spring-boot-starter-web jar file in dependency then it will create all configurations required for web application like DisplatcherServlet, etc.. It uses servlet 3.0 annotation based configuration instead of writing web.xml and spring related context xml files. It will comes with embedded Tomcat server to run the spring-boot application.
Creating Spring Boot Application
1) Using STS IDE:
We need to provide the Project Name, GroupId, ArtifactId and description.
We can select Type either Maven or Gradle as build tools.
We can select the spring-boot- version, and we can select what are the different modules we want to use in our application.
2) Using Spring.io website:
We can select Build tool, Spring Boot version, and list of spring various modules (like web, security, Jersey etc...) and click on Generate Project, it will download zip file, you can extract and we can import the project into STS IDE.
3) Using Maven Parent:
- Create a maven project.
- Specify parent as given below in pox.xml file.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/><! --Lookup parent from repository -->
</parent>
- We can specify the spring starter dependencies without specifying version, version values will take from parent.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- We need to add the spring-boot maven plugin manually.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
4) Using Maven without depending on parent attribute:
Maven provides bill of Materials(BOM) concept, we can specify the spring-boot BOM dependency in pom.xml.
<dependencyManagement>
<dependencies>
<dependency>
<! -- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
And we can specify the various spring-boot-starter dependencies and spring-boot maven plugin as described above.
After importing the project into STS, it will comes with Main java class.
The snippet of the main class is:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot provides annotations which will bootstrap the application without writing any code.
@SpringBootApplication is the annotation is responsible to scan the current package and creates the beans.
And it will do auto-configuration based on class-path.
Using attributes we can customize the configuration.
Eg: it has attribute “scanBasePackages”, if we can specify the value spring-boot limit the scan for beans in the specified packages only.
Alternatives to @SpringBootApplication:
We can annotate the class with @Configuration,@EnableAutoConfiguration and
@ComponentScan which will do the same as @SpringBootApplication
Spring boot comes spring-boot-autoconfigure jar file, which contains all required configuration for bootstrap the different spring modules.
If you want to disable auto-configure nature then we can specify it in EnableAutoConfiguration.
Eg: if we want to disable data-source auto configuration we can DataSourceAutoConfiguration in exclude attribute of EnableAutoConfiguration.
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
Creating ApplicationContext Hierarchy using Spring-Boot:
Spring-boot is providing SpringApplicationBuilder class, using this class we can create applicationContext Hierarchy.
Example:
SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
Parent is the spring configuration file which has bean declarations in it, Application also the spring configuration file which is source of bean configuration. Parent file will creates a applicationContext, which is parent to all applicationContext. Application also creates new applicationContext which is a child above context, beans present in Parent will be shared by all child beans.
Running Spring Boot Application
If the application spring-starter-web as dependency then application will start the embedded tomcat server and deploy our application otherwise it will the start the main class and it will terminate.
If the application doesn`t contain web as dependency then it terminate after executing main class.
Or we can run the application as java application.
Which will also do the same.
Preparing runnable jar using spring-boot maven plugin:
Spring boot maven plugin (additional features to default maven plugin) will provides various goals.
Drawbacks with default maven plugin: maven provides package as the goal to provide the application jar file, which is not a runnable jar because it will not contains all required dependencies, but spring-boot maven plugin provides repackage goal which will do this.
Eg: mvn package spring-boot:repackage
Maven goal will prepare the application jar file and also all required jar files and prepare the final runnable jar file.
using java we can run the jar file.
java -jar . Here no need to specify the classpath.
Prepaing war for Spring-Boot applications:
We can prepare the war file for our application to deploy the application in external application servers like weblogic, websphere etc...
Changes required in pom.xml to prepare war file:
- we need to change the packing from "jar" to "war".
- We need to add the dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
By default spring-boot-starter-web has this dependency, but we need to override the scope of the jar file.
- We can use mvn package spring-boot:repackage to prepare the war file.
We can run spring boot applications has windows background services, and we can deploy applications to cloud like cloud-factory and Heroku.
Upgrading from lower version to current version:
- we need to change spring-boot-parent version, it will automatically upgrade all other spring related dependencies.
- We can change spring-boot-dependencies version to current spring-boot version.
Changing java version:
Pom.xml file contains java.version property, this we can update to current java version.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
Conclusion
If we use Spring Boot in Java applications we can more focus on business instead of developing platform, version conflicts with other API, Spring boot comes with embedded server so that we can run and we can test application easily.
Hope this blog is helpful in creating and running Spring boot applications.
Author Bio
James warner is senior java application development. He works as a developers in Nexsoftsys. He major contribute included the collection web services and Java framework. He is extensive experience as java technology and object oriented programming language. You can follow me on twitter @nexsoftsys.