03 March 2018

Spring Boot Application

Spring boot framework:

1.Helps to build applications rapidly by removing the need to configure boiler plate configuration code.

2.Though it has no additional features to add to spring framework, It helps to build production
 ready applications quickly.

3.It provides necessary endpoints out of the box to check metrics, health check with spring boot actuator module.

4.Embedded servers(tomcat,jetty) and embedded databases can be added to build the application
without looking into the database configuration details.

We can build a simple HelloWorld Spring MVC application with Spring Boot in 2 minutes.

Project structure:  It does not need web.xml. Spring boot will take care of these configurations.

spring boot
Add caption


pom.xml 

<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>java9pro</groupId>
    <artifactId>java9pro.springboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>


</project>

LoginController:

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

@Controller
@EnableAutoConfiguration
public class LoginController {


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

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

It has annotation @EnableAutoConfiguration, which will load the required jar dependencies.It will
initialize the application context, registers the dispatcher servlet, registers the beans for JMX.

After running the main method of LoginController, Application will start on the embedded tomcat on default port 8080.

Output can be viewed on this URL.

http://localhost:8080

server port can be changed by adding configuration to application.properties file in resources.

server.port = 8090.

Then server starts on port 8090 and URL http://localhost:8090 will give the result.







No comments: