Spring Boot makes very easy to build standalone as well as production grade Spring based application. I found features like "Embedded Tomcat and Jetty. no need to deploy war. also XML based configuration eliminated" - interesting !
Spring Boot Features :
If you are using Maven then add following into pom.xml
If You are using Gradle then Add following dependencies into build.gradle
Build Sample Spring Application using Spring Boot :
Home Controller
Here, @RestController is newly introduced in Spring 4 while previously using @Controller in spring 3.2. @RestController combines @Controller and @ResponseBody hence due to same, then we hit request from browser, it returns data rather than View.
Application
@ComponentScan tells Spring to look/scan components/services/config into package "com.anuj"
@Configuration marks class as Config class, Source of bean definations
build.gradle
Run Application from eclipse and hit http://localhost:8080/home into Browser
Output :
Spring Boot Features :
- Create Stand Alone Spring based Application
- No need for XML Configuration
- Embedded Tomcat and Jetty (no need to deploy war)
- Configure Spring Automatically whenever possible
- Maven Configuration simplified
- Provide production ready features like health check and externalized Configuration.
If you are using Maven then add following into pom.xml
org.springframework.boot spring-boot-starter-parent 1.0.1.RELEASE org.springframework.boot spring-boot-starter-web
If You are using Gradle then Add following dependencies into build.gradle
dependencies { compile("org.springframework.boot:spring-boot-starter-web:1.0.1.RELEASE") }
Build Sample Spring Application using Spring Boot :
Home Controller
package com.anuj.controllers; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; /** * * @author Anuj * @source goldenpackagebyanuj.blogspot.com */ @RestController public class HomeController { @RequestMapping("/home") public String handleHome() { return "Welcome to Spring Boot!"; } }
Here, @RestController is newly introduced in Spring 4 while previously using @Controller in spring 3.2. @RestController combines @Controller and @ResponseBody hence due to same, then we hit request from browser, it returns data rather than View.
Application
package com.anuj; import java.util.Arrays; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * * @author Anuj * @source goldenpackagebyanuj.blogspot.com */ @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); System.out.println("Beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } } }Here, @EnableAutoConfiguration tells Spring to start adding beans based on classpath settings,other beans etc.
@ComponentScan tells Spring to look/scan components/services/config into package "com.anuj"
@Configuration marks class as Config class, Source of bean definations
build.gradle
buildscript { repositories { maven { url "http://repo.spring.io/libs-snapshot" } mavenLocal() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.1.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' jar { baseName = 'SpringBoot' version = '1.0' } repositories { mavenCentral() maven { url "http://repo.spring.io/libs-snapshot" } } dependencies { // tag::jetty[] compile("org.springframework.boot:spring-boot-starter-web") { exclude module: "spring-boot-starter-tomcat" } compile("org.springframework.boot:spring-boot-starter-jetty") // end::jetty[] // tag::actuator[] compile("org.springframework.boot:spring-boot-starter-actuator") // end::actuator[] testCompile("junit:junit") } task wrapper(type: Wrapper) { gradleVersion = '1.9' }
Run Application from eclipse and hit http://localhost:8080/home into Browser
Output :
No comments:
Post a Comment