Category Archives: Java framework

SPRINGBOOT

SpringBoot

Spring is a very popular Java based framework for building web and enterprise applications. Unlike many other frameworks which focuses on only one area, Spring framework provides a wide variety of features addressing the modern business needs through its portfolio projects. Spring framework provides flexibility to configure the beans in multiple ways such as XML, Annotations, and JavaConfig. With the number of features increased the complexity also gets increases and configuring Spring applications becomes tedious and error-prone. Spring team created SpringBoot to address the complexity of configuration. Explore this article and know more about SpringBoot.

Overview of Spring framework

If you are a Java developer, then there is a high chance that you might have heard about Spring framework and probably have used it in your projects. Spring framework was initially created as a Dependency Injection container but we all know it is much more than that.

Spring is very popular because of various reasons

  • Spring’s dependency injection approach encourages the writing testable code.
  • Spring simplifies integration with other Java frameworks like JPA/Hibernate ORM, Struts/JSF, and other web frameworks
  • State of the art Web MVC framework for building web applications

Along with Spring framework there are many other Spring sister projects which helps to build applications addressing modern business needs:

SPRINGBOOT

Spring Data : Simplifies data access from relational and NoSQL data stores.

Spring Batch : Provides powerful batch processing framework.

Spring Security : Robust security framework to secure applications.

Spring Social : Supports integration with social networking sites such as Facebook, Twitter, LinkedIn, GitHub, and much more.

Spring Integration : An implementation of Enterprise Integration Patterns to facilitate integration with other enterprise applications using lightweight messaging and declarative adapters.

There are many other interesting projects addressing various other modern application development needs.

In the initial days, Spring framework has provided XML based approach for configuring beans. Later Spring has introduced XML based DSLs, Annotations, and JavaConfig based approaches for configuring beans.

A quick taste of SpringBoot

Welcome to SpringBoot! SpringBoot do what exactly you are looking for. It will do things automatically for the user and allows them to override the defaults if they want to.

Instead of explaining in theory let’s see by example.

Step 1: Create a Maven based SpringBoot Project

Create a Maven project and configure the dependencies as follows:

?

<?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/maven-v4_0_0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>com.sivalabs</groupId>

<artifactId>hello-springboot</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>hello-springboot</name>

<parent>

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

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

<version>1.3.2.RELEASE</version>

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

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

</properties>

<dependencies>

<dependency>

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

<artifactId>spring-boot-starter-test</artifactId>

</dependency>

<dependency>

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

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

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

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

</dependency>

<dependency>

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

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

<dependency>

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

<artifactId>spring-boot-devtools</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

</dependencies>

</project>

Wow our pom.xml suddenly become so small!!.

Step 2: Configure datasource/JPA properties in application.properties in src/main/resources as follows.

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=admin

spring.datasource.initialize=true

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

Step 3: Create a JPA Entity and Spring Data JPA Repository Interface for the entity.

Create User.java, UserRepository.java and HomeController.java same as in springmvc-jpa-demo application.

Step 4: Create Thymeleaf view to show list of users

Copy /WEB-INF/views/index.html that we created in springmvc-jpa-demo application into src/-main/resources/templates folder in our new project.

Step 5: Create SpringBoot EntryPoint Class

Create a Java class Application.java with main method as follows:

@SpringBootApplication

public class Application

{

public static void main(String[] args)

{

SpringApplication.run(Application.class, args);

}

}

Now run Application.java as a Java Application and point your browser to

http://localhost:8080/.