opencodez

Spring Boot – Java Development Made Easy

If you are planning for a Java web application, first thing you will decide is to use Spring framework. This framework is best known for its Inversion Of Controller container and along with itxs extensive support on MVC, JDBC, JMS, Aspect and many more fronts which you may need while developing a web application. Few years back the Pivotal introduced world to Spring Boot. This has changed the way Java development and deployment is done.

Prior to Spring Boot, even though you are using Spring Framework you have to go through xml based configuration to make sure your application starts up fine. After that you would need to setup an application server to deploy your artifact and test if its working as expected. Spring Boot comes with auto configurations for all types of project and it comes with embedded tomcat server. Due to this you can run and test your application right from your Eclipse IDE.

Here we will see how simple is it to develop Java Web MVC application using Spring Boot and Maven.

Create a Maven project and add below dependency to your POM file to get power of spring boot to your project.

xparentx
    xgroupIdxorg.springframework.bootx/groupIdx
    xartifactIdxspring-boot-starter-parentx/artifactIdx
    xversionx1.4.0.RELEASEx/versionx
x/parentx
xdependenciesx
    xdependencyx
        xgroupIdxorg.springframework.bootx/groupIdx
        xartifactIdxspring-boot-starter-webx/artifactIdx
    x/dependencyx
x/dependenciesx

Above dependency spring-boot-starter-web will add all required libraries for a web mvc application to your projectxs classpath, you donxt have to look for any other dependency. After that add below class to your project.

package my.spring.boot;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@Controller
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

The annotation SpringBootApplication will configure your project to be an web application. You can see we have also added annotation Controller to define one method for mapping to home url of application. This method will return plain text xHello World!x in browser.

By default boot comes with embedded tomcat that runs on port 8080. Just right click on above file in your IDE and run as Java. You will see in console Spring Boot starting up and a message that your application is started. Once you get this message simply go to browser and type http://locahost:8080.

Thatxs it you application is up and running in less that 10 seconds. Isnxt it great to see your web application running so fast. Spring Boot has definitely changed the way development is happening and it has reduced the development efforts considerably.

Spring has also provided an online utility to create a project template as per your need. Spring Starter, using this you can add features to your project and download the skeleton which you can use and add more functionalities as per your needs.

We at Opencodez will try to explore Spring Boot and post many tutorials, codes for our readers that we are sure going to help in some way or the other. You can refer many more such articles like below

x