SlideShare a Scribd company logo
Spring 
Boot 
By 
Bhagwat 
Kumar
Agenda 
• What 
and 
Why? 
• Key 
features 
of 
Spring 
boot 
• Prototyping 
using 
CLI. 
• Gradle 
primer 
• Managing 
profiles 
aka 
environment 
in 
grails 
• Using 
Spring 
data 
libraries 
e.g. 
MongoDB 
• Using 
GORM 
• Presentation 
layer 
• Using 
GSP 
2
What 
and 
why? 
• Its 
not 
a 
replacement 
for 
Spring 
framework 
but 
it 
presents 
a 
small 
surface 
area 
for 
users 
to 
approach 
and 
extract 
value 
from 
the 
rest 
of 
Spring. 
• Spring-­‐boot 
provides 
a 
quick 
way 
to 
create 
a 
Spring 
based 
application 
from 
dependency 
management 
to 
convention 
over 
configuration. 
• Grails 
3.0 
will 
be 
based 
on 
Spring 
Boot. 
image 
source 
: 
http://spring.io/blog/2013/08/06/spring-­‐boot-­‐simplifying-­‐spring-­‐for-­‐everyone 
3
Key 
Features 
• Stand-­‐alone 
Spring 
applications 
• No 
code 
generation/ 
No 
XML 
Config 
• Automatic 
configuration 
by 
creating 
sensible 
defaults 
• Starter 
dependencies 
• Structure 
your 
code 
as 
you 
like 
• Supports 
Gradle 
and 
Maven 
• Common 
non-­‐functional 
requirements 
for 
a 
"real" 
application 
– 
embedded 
servers, 
– 
security, 
metrics, 
health 
checks 
– 
externalised 
configuration
Rapid 
Prototyping 
: 
Spring 
CLI 
• Quickest 
way 
to 
get 
a 
spring 
app 
off 
the 
ground 
• Allows 
you 
to 
run 
groovy 
scripts 
without 
much 
boilerplate 
code 
• Not 
recommended 
for 
production 
Install using GVM 
$ gvm install springboot 
Running groovy scripts 
$ spring run app.groovy 
$ spring run --watch app.groovy 
$ spring test tests.groovy
A 
quick 
web 
application 
using 
spring 
boot 
app.groovy 
@Controller 
class 
Example 
{ 
@RequestMapping("/") 
@ResponseBody 
public 
String 
helloWorld() 
{ 
"Hello 
Spring 
boot 
audience!!!" 
} 
} 
$ 
spring 
run 
app.groovy
What 
Just 
happened? 
// 
import 
org.springframework.web.bind.annotation.Controller 
// 
other 
imports 
... 
// 
@Grab("org.springframework.boot:spring-­‐boot-­‐web-­‐starter:0.5.0") 
// 
@EnableAutoConfiguration 
@Controller 
class 
Example 
{ 
@RequestMapping("/") 
@ResponseBody 
public 
String 
hello() 
{ 
return 
"Hello 
World!"; 
} 
// 
public 
static 
void 
main(String[] 
args) 
{ 
// 
SpringApplication.run(Example.class, 
args); 
// 
} 
}
Starter 
POMs 
• One-­‐stop-­‐shop 
for 
all 
the 
Spring 
and 
related 
technology 
• A 
set 
of 
convenient 
dependency 
descriptors 
• Contain 
a 
lot 
of 
the 
dependencies 
that 
you 
need 
to 
get 
a 
project 
up 
and 
running 
quickly 
• All 
starters 
follow 
a 
similar 
naming 
pattern; 
– 
spring-­‐boot-­‐starter-­‐* 
• Examples 
– spring-­‐boot-­‐starter-­‐web 
– spring-­‐boot-­‐starter-­‐data-­‐rest 
– spring-­‐boot-­‐starter-­‐security 
– spring-­‐boot-­‐starter-­‐amqp 
– spring-­‐boot-­‐starter-­‐data-­‐jpa 
– spring-­‐boot-­‐starter-­‐data-­‐elasticsearch 
– spring-­‐boot-­‐starter-­‐data-­‐mongodb 
– spring-­‐boot-­‐starter-­‐actuator
Demo 
: 
Starter 
POMs 
@Grab('spring-­‐boot-­‐starter-­‐security') 
@Grab('spring-­‐boot-­‐starter-­‐actuator') 
@Controller 
class 
Example 
{ 
@RequestMapping("/") 
@ResponseBody 
public 
String 
helloWorld() 
{ 
return 
"Hello 
Audience!!!" 
} 
} 
//security.user.name 
: 
default 
'user' 
//security.user.password 
: 
see 
log 
for 
auto 
generated 
password 
//actuator 
endpoints: 
/beans, 
/health, 
/mappings, 
/metrics 
etc.
Building 
using 
Gradle 
10
Lets 
go 
beyond 
prototyping 
: 
Gradle 
Image 
source 
: 
http://www.drdobbs.com/jvm/why-­‐build-­‐your-­‐java-­‐projects-­‐with-­‐gradle/240168608
build.gradle 
task 
hello 
<< 
{ 
println 
"Hello 
!!!!" 
} 
task 
greet 
<<{ 
println 
"Welocome 
Mr. 
Kumar" 
} 
task 
intro(dependsOn: 
hello) 
<< 
{ 
println 
"I'm 
Gradle" 
} 
hello 
<< 
{ 
println 
"Hello 
extended!!!!" 
} 
greet.dependsOn 
hello, 
intro 
// 
gradle 
tasks 
:list 
all 
the 
available 
tasks 
// 
gradle 
intro 
:executes 
intro 
task 
// 
gradle 
-­‐q 
greet 
:bare 
build 
output 
// 
gradle 
-­‐-­‐daemon 
hello 
:subsequent 
execution 
will 
be 
fast
build.gradle 
: 
using 
plugin 
and 
adding 
dependencies 
apply 
plugin: 
"groovy" 
//look 
for 
sources 
in 
src/main/groovy 
folder 
//inherits 
java 
plugin: 
src/main/java 
folder 
// 
tasks 
compileJava, 
compileGroovy, 
build, 
clean 
sourceCompatibility 
= 
1.6 
repositories 
{ 
mavenCentral() 
} 
dependencies 
{ 
compile 
'org.codehaus.groovy:groovy-­‐all:2.3.6' 
compile 
"org.apache.commons:commons-­‐lang3:3.0.1" 
testCompile 
"junit:unit:4.+" 
}
build.gradle: 
for 
Spring 
boot 
app 
with 
hot 
reloading 
apply 
plugin: 
'groovy' 
apply 
plugin: 
'idea' 
apply 
plugin: 
'spring-­‐boot' 
buildscript 
{ 
repositories 
{ 
mavenCentral()} 
dependencies 
{ 
classpath("org.springframework.boot:spring-­‐boot-­‐gradle-­‐plugin:1.1.8.RELEASE") 
classpath 
'org.springframework:springloaded:1.2.0.RELEASE' 
} 
} 
idea 
{ 
module 
{ 
inheritOutputDirs 
= 
false 
outputDir 
= 
file("$buildDir/classes/main/") 
} 
} 
repositories 
{ 
mavenCentral() 
} 
dependencies 
{ 
compile 
'org.codehaus.groovy:groovy-­‐all' 
compile 
'org.springframework.boot:spring-­‐boot-­‐starter-­‐web' 
}
Environment 
and 
Profile 
aka 
Grails 
config 
• Put 
application.properties/application.yml 
somewhere 
in 
classpath 
• Easy 
one: 
src/main/resources 
folder 
15 
application.yml 
app: 
name: 
Springboot+Config+Yml+Demo 
version: 
1.0.0 
server: 
port: 
8080 
settings: 
counter: 
1 
-­‐-­‐-­‐ 
spring: 
profiles: 
development 
server: 
port: 
9001 
application.properties 
app.name=Springboot+Config+Demo 
app.version=1.0.0 
server.port=8080 
settings.coutner=1 
application-­‐development.properties 
app.name=Springboot+Config+Demo 
app.version=1.0.0 
server.port=8080
Binding 
properties 
16 
Using 
ConfigurationProperties 
annotation 
import 
org.springframework.boot.context.properties.ConfigurationProperties 
import 
org.springframework.stereotype.Component 
@Component 
@ConfigurationProperties(prefix 
= 
"app") 
class 
AppInfo 
{ 
String 
name 
String 
version 
} 
Using 
Value 
annotation 
import 
org.springframework.beans.factory.annotation.Value 
import 
org.springframework.stereotype.Component 
@Component 
class 
AppConfig 
{ 
@Value('${app.name}') 
String 
appName 
@Value('${server.port}') 
Integer 
port 
}
Examples 
17 
OS 
env 
variable 
export 
SPRING_PROFILES_ACTIVE=development 
export 
SERVER_PORT=8090 
gradle 
bootRun 
java 
-­‐jar 
build/libs/demo-­‐1.0.0.jar 
with 
a 
-­‐D 
argument 
(remember 
to 
put 
it 
before 
the 
main 
class 
or 
jar 
archive) 
java 
-­‐jar 
-­‐Dspring.profiles.active=development 
build/libs/dem-­‐1.0.0.jar 
java 
-­‐jar 
-­‐Dserver.port=8090 
build./libs/demo-­‐1.0.0.jar
Using 
Spring 
data 
• Add 
dependency 
compile 
'org.springframework.boot:spring-­‐boot-­‐starter-­‐data-­‐mongodb' 
• Configure 
database 
URL 
spring.data.mongodb.uri=mongodb://localhost/springtestdev 
• Add 
entity 
class 
import 
org.springframework.data.annotation.Id; 
class 
• Add 
Person{@Id 
String 
id, 
String 
name} 
repository 
interface 
import 
org.springframework.data.mongodb.repository.MongoRepository 
public 
interface 
PersonRepository 
extends 
• Autowire 
and 
use 
like 
charm 
MongoRepository<Person, 
String> 
{} 
@Autowired 
PersonRepository 
personRepository 
personRepository.save(new 
Person(name:'Spring 
Boot')) 
personRepository.findAll() 
personRepository.count()
Next 
level 
persistence 
with 
GORM 
• Add 
dependencies 
to 
use 
GORM-­‐Hibernate 
• For 
GORM 
MongoDB 
use 
the 
following 
dependencies 
compile 
'org.grails:gorm-­‐mongodb-­‐spring-­‐boot:1.1.0.RELEASE' 
• Add 
entity 
with 
@grails.persistence.entity 
19 
compile 
'org.springframework.boot:spring-­‐boot-­‐starter-­‐data-­‐jpa' 
compile 
'org.grails:gorm-­‐hibernate4-­‐spring-­‐boot:1.1.0.RELEASE' 
runtime 
'com.h2database:h2' 
//for 
h2 
database 
import 
grails.persistence.Entity 
@Entity 
class 
Person 
{ 
String 
name; 
Integer 
age 
static 
constraints 
= 
{ 
name 
blank: 
false 
age 
min: 
15 
} 
} 
Further 
reading 
https://github.com/grails/grails-­‐data-­‐mapping
Server 
side 
view 
template 
libraries 
• JSP/JSTL 
• Thymeleaf 
• Freemarker 
• Velocity 
• Tiles 
• GSP 
• Groovy 
Template 
Engine 
20
• Very 
limited 
existing 
tags 
available 
• https://github.com/grails/grails-­‐boot/issues/3 
• Add 
dependency 
• Put 
GSP 
templates 
in 
resources/templates 
folder 
GSP 
21 
compile 
"org.grails:grails-­‐gsp-­‐spring-­‐boot:1.0.0.RC1" 
compile 
"org.grails:grails-­‐web:2.4.0.M2"
GSP 
continued... 
• Sample 
request 
handler 
22 
@RequestMapping("/show/{id}") 
public 
ModelAndView 
show(@PathVariable 
Long 
id) 
{ 
Person 
person 
= 
Person.read(id) 
if 
(person) 
{ 
//render(view:"/person/show", 
model:[personInstance:personInstance]) 
new 
ModelAndView("/person/show", 
[personInstance: 
Person.get(id)]) 
} 
else 
{ 
log.info 
"No 
entity 
fount 
for 
id 
: 
" 
+ 
id 
//redirect(controller:"person", 
action:"list") 
new 
ModelAndView("redirect:/person/list") 
} 
}
Grails 
Taglib 
23 
@grails.gsp.TagLib 
@org.springframework.stereotype.Component 
class 
ApplicationTagLib 
{ 
static 
namespace 
= 
"app" 
def 
paginate 
= 
{ 
attrs 
-­‐> 
String 
action 
= 
attrs.action 
Integer 
total 
= 
attrs.total 
Integer 
currentPage 
= 
attrs.currentPage 
?: 
1 
Integer 
pages 
= 
(total 
/ 
10) 
+ 
1 
out 
<< 
render(template: 
"/shared/pagination", 
model: 
[action: 
action, 
total: 
total, 
currentPage: 
currentPage, 
pages: 
pages] 
) 
} 
} 
<app:paginate 
total="${personInstanceCount 
?: 
0}" 
currentPage="${currentPage}" 
action="/person/list"/>
Packaging 
executable 
jar 
and 
war 
files 
24 
Packaging 
as 
jar 
with 
embedded 
tomcat 
$ 
gradle 
build 
$ 
java 
-­‐jar 
build/libs/mymodule-­‐0.0.1-­‐SNAPSHOT.jar 
Packaging 
as 
war 
: 
configure 
build.groovy 
//... 
apply 
plugin: 
'war' 
war 
{ 
baseName 
= 
'myapp' 
version 
= 
'0.5.0' 
} 
//.... 
configurations 
{ 
providedRuntime 
} 
dependencies 
{ 
compile("org.springframework.boot:spring-­‐boot-­‐starter-­‐web") 
providedRuntime("org.springframework.boot:spring-­‐boot-­‐starter-­‐tomcat") 
// 
... 
} 
$ 
gradle 
war
Q/A 
25
Thank 
you. 
26 
Blog: 
http://www.intelligrape.com/blog/author/bhagwat 
LikedIn: 
http://www.linkedin.com/in/bhagwatkumar 
Twitter: 
http://twitter.com/bhagwatkumar 
Mail 
: 
bhagwat@intelligrape.com
References 
Samples 
: 
https://github.com/bhagwat/spring-­‐boot-­‐samples 
http://docs.spring.io/spring-­‐boot/docs/current-­‐SNAPSHOT/reference/htmlsingle 
http://docs.spring.io/spring-­‐boot/docs/current-­‐SNAPSHOT/reference/htmlsingle/#getting-­‐ 
started-­‐gvm-­‐cli-­‐installation 
https://github.com/spring-­‐projects/spring-­‐boot/tree/master/spring-­‐boot-­‐cli/samples 
http://docs.spring.io/spring-­‐boot/docs/current-­‐SNAPSHOT/reference/htmlsingle/#using-­‐boot-­‐ 
starter-­‐poms 
http://spring.io/guides/gs/accessing-­‐mongodb-­‐data-­‐rest/ 
https://spring.io/guides/gs/accessing-­‐data-­‐mongodb/ 
https://spring.io/guides/gs/accessing-­‐data-­‐jpa/ 
http://www.gradle.org/ 
http://www.slideshare.net/Soddino/developing-­‐an-­‐application-­‐with-­‐spring-­‐boot-­‐34661781 
http://presos.dsyer.com/decks/spring-­‐boot-­‐intro.html 
http://pygments.org/ 
for 
nicely 
formatting 
code 
snippets 
included 
in 
presentation 
27

More Related Content

PPTX
Spring boot
PPTX
Introduction to Spring Boot
PPTX
Spring boot Introduction
ODP
Xke spring boot
PPT
Spring Boot in Action
PPTX
Introduction to spring boot
PDF
Spring Framework - Core
PPTX
Spring boot
Spring boot
Introduction to Spring Boot
Spring boot Introduction
Xke spring boot
Spring Boot in Action
Introduction to spring boot
Spring Framework - Core
Spring boot

What's hot (20)

PDF
Spring boot introduction
PDF
Spring Framework - AOP
PDF
Spring Boot
PPTX
Spring boot - an introduction
PPT
Spring Core
PPTX
Spring Framework
PPTX
Spring Boot
PPTX
Spring Boot Tutorial
PPTX
Introduction to Spring Framework
PDF
Spring Boot
PDF
Spring Framework
PDF
Introduction to Spring Boot!
PDF
Spring Boot & Actuators
PDF
REST APIs with Spring
PDF
Spring Boot
PPTX
Spring boot
PPTX
Spring data jpa
PDF
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
PDF
Gradle Introduction
PPTX
Spring Boot and REST API
Spring boot introduction
Spring Framework - AOP
Spring Boot
Spring boot - an introduction
Spring Core
Spring Framework
Spring Boot
Spring Boot Tutorial
Introduction to Spring Framework
Spring Boot
Spring Framework
Introduction to Spring Boot!
Spring Boot & Actuators
REST APIs with Spring
Spring Boot
Spring boot
Spring data jpa
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
Gradle Introduction
Spring Boot and REST API
Ad

Viewers also liked (16)

PDF
That old Spring magic has me in its SpEL
KEY
Modular Java - OSGi
PDF
Shootout! template engines on the jvm
PDF
Java Configuration Deep Dive with Spring
PDF
Boot It Up
PPT
Spring MVC
PDF
Spring MVC
PDF
Building RESTful applications using Spring MVC
PDF
Spring Framework 4.0 to 4.1
PDF
Spring Mvc Rest
PPTX
Spring Web MVC
PDF
Spring Web Services: SOAP vs. REST
PDF
Microservices with Spring Boot
PDF
RESTful Web Services with Spring MVC
PPT
Presentation Spring
PDF
Shootout! Template engines for the JVM
That old Spring magic has me in its SpEL
Modular Java - OSGi
Shootout! template engines on the jvm
Java Configuration Deep Dive with Spring
Boot It Up
Spring MVC
Spring MVC
Building RESTful applications using Spring MVC
Spring Framework 4.0 to 4.1
Spring Mvc Rest
Spring Web MVC
Spring Web Services: SOAP vs. REST
Microservices with Spring Boot
RESTful Web Services with Spring MVC
Presentation Spring
Shootout! Template engines for the JVM
Ad

Similar to Spring boot (20)

PPTX
Grails Spring Boot
PPTX
Spring boot 3g
PDF
Spring boot wednesday
PDF
Spring boot
PPTX
PDF
JVM Web Frameworks Exploration
PDF
Spring boot jpa
PPTX
Spring boot
PPTX
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
PDF
Connecting Connect with Spring Boot
PDF
Spring Boot and Microservices
PDF
SpringBoot
PDF
Spring Boot
PDF
Rediscovering Spring with Spring Boot(1)
PDF
Java-Vienna: Spring Boot 1.x Overview/Intro by Dominik Dorn
PDF
GR8Conf 2015 - Spring Boot and Groovy. What more do you need?
PDF
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
PDF
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
PDF
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
PPT
Spring Boot Introduction and framework.ppt
Grails Spring Boot
Spring boot 3g
Spring boot wednesday
Spring boot
JVM Web Frameworks Exploration
Spring boot jpa
Spring boot
Станислав Сидоренко «DeviceHive Java Server – миграция на Spring Boot»
Connecting Connect with Spring Boot
Spring Boot and Microservices
SpringBoot
Spring Boot
Rediscovering Spring with Spring Boot(1)
Java-Vienna: Spring Boot 1.x Overview/Intro by Dominik Dorn
GR8Conf 2015 - Spring Boot and Groovy. What more do you need?
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Spring Boot Introduction and framework.ppt

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Paper A Mock Exam 9_ Attempt review.pdf.
What if we spent less time fighting change, and more time building what’s rig...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RMMM.pdf make it easy to upload and study
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
Yogi Goddess Pres Conference Studio Updates
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India

Spring boot

  • 1. Spring Boot By Bhagwat Kumar
  • 2. Agenda • What and Why? • Key features of Spring boot • Prototyping using CLI. • Gradle primer • Managing profiles aka environment in grails • Using Spring data libraries e.g. MongoDB • Using GORM • Presentation layer • Using GSP 2
  • 3. What and why? • Its not a replacement for Spring framework but it presents a small surface area for users to approach and extract value from the rest of Spring. • Spring-­‐boot provides a quick way to create a Spring based application from dependency management to convention over configuration. • Grails 3.0 will be based on Spring Boot. image source : http://spring.io/blog/2013/08/06/spring-­‐boot-­‐simplifying-­‐spring-­‐for-­‐everyone 3
  • 4. Key Features • Stand-­‐alone Spring applications • No code generation/ No XML Config • Automatic configuration by creating sensible defaults • Starter dependencies • Structure your code as you like • Supports Gradle and Maven • Common non-­‐functional requirements for a "real" application – embedded servers, – security, metrics, health checks – externalised configuration
  • 5. Rapid Prototyping : Spring CLI • Quickest way to get a spring app off the ground • Allows you to run groovy scripts without much boilerplate code • Not recommended for production Install using GVM $ gvm install springboot Running groovy scripts $ spring run app.groovy $ spring run --watch app.groovy $ spring test tests.groovy
  • 6. A quick web application using spring boot app.groovy @Controller class Example { @RequestMapping("/") @ResponseBody public String helloWorld() { "Hello Spring boot audience!!!" } } $ spring run app.groovy
  • 7. What Just happened? // import org.springframework.web.bind.annotation.Controller // other imports ... // @Grab("org.springframework.boot:spring-­‐boot-­‐web-­‐starter:0.5.0") // @EnableAutoConfiguration @Controller class Example { @RequestMapping("/") @ResponseBody public String hello() { return "Hello World!"; } // public static void main(String[] args) { // SpringApplication.run(Example.class, args); // } }
  • 8. Starter POMs • One-­‐stop-­‐shop for all the Spring and related technology • A set of convenient dependency descriptors • Contain a lot of the dependencies that you need to get a project up and running quickly • All starters follow a similar naming pattern; – spring-­‐boot-­‐starter-­‐* • Examples – spring-­‐boot-­‐starter-­‐web – spring-­‐boot-­‐starter-­‐data-­‐rest – spring-­‐boot-­‐starter-­‐security – spring-­‐boot-­‐starter-­‐amqp – spring-­‐boot-­‐starter-­‐data-­‐jpa – spring-­‐boot-­‐starter-­‐data-­‐elasticsearch – spring-­‐boot-­‐starter-­‐data-­‐mongodb – spring-­‐boot-­‐starter-­‐actuator
  • 9. Demo : Starter POMs @Grab('spring-­‐boot-­‐starter-­‐security') @Grab('spring-­‐boot-­‐starter-­‐actuator') @Controller class Example { @RequestMapping("/") @ResponseBody public String helloWorld() { return "Hello Audience!!!" } } //security.user.name : default 'user' //security.user.password : see log for auto generated password //actuator endpoints: /beans, /health, /mappings, /metrics etc.
  • 11. Lets go beyond prototyping : Gradle Image source : http://www.drdobbs.com/jvm/why-­‐build-­‐your-­‐java-­‐projects-­‐with-­‐gradle/240168608
  • 12. build.gradle task hello << { println "Hello !!!!" } task greet <<{ println "Welocome Mr. Kumar" } task intro(dependsOn: hello) << { println "I'm Gradle" } hello << { println "Hello extended!!!!" } greet.dependsOn hello, intro // gradle tasks :list all the available tasks // gradle intro :executes intro task // gradle -­‐q greet :bare build output // gradle -­‐-­‐daemon hello :subsequent execution will be fast
  • 13. build.gradle : using plugin and adding dependencies apply plugin: "groovy" //look for sources in src/main/groovy folder //inherits java plugin: src/main/java folder // tasks compileJava, compileGroovy, build, clean sourceCompatibility = 1.6 repositories { mavenCentral() } dependencies { compile 'org.codehaus.groovy:groovy-­‐all:2.3.6' compile "org.apache.commons:commons-­‐lang3:3.0.1" testCompile "junit:unit:4.+" }
  • 14. build.gradle: for Spring boot app with hot reloading apply plugin: 'groovy' apply plugin: 'idea' apply plugin: 'spring-­‐boot' buildscript { repositories { mavenCentral()} dependencies { classpath("org.springframework.boot:spring-­‐boot-­‐gradle-­‐plugin:1.1.8.RELEASE") classpath 'org.springframework:springloaded:1.2.0.RELEASE' } } idea { module { inheritOutputDirs = false outputDir = file("$buildDir/classes/main/") } } repositories { mavenCentral() } dependencies { compile 'org.codehaus.groovy:groovy-­‐all' compile 'org.springframework.boot:spring-­‐boot-­‐starter-­‐web' }
  • 15. Environment and Profile aka Grails config • Put application.properties/application.yml somewhere in classpath • Easy one: src/main/resources folder 15 application.yml app: name: Springboot+Config+Yml+Demo version: 1.0.0 server: port: 8080 settings: counter: 1 -­‐-­‐-­‐ spring: profiles: development server: port: 9001 application.properties app.name=Springboot+Config+Demo app.version=1.0.0 server.port=8080 settings.coutner=1 application-­‐development.properties app.name=Springboot+Config+Demo app.version=1.0.0 server.port=8080
  • 16. Binding properties 16 Using ConfigurationProperties annotation import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.stereotype.Component @Component @ConfigurationProperties(prefix = "app") class AppInfo { String name String version } Using Value annotation import org.springframework.beans.factory.annotation.Value import org.springframework.stereotype.Component @Component class AppConfig { @Value('${app.name}') String appName @Value('${server.port}') Integer port }
  • 17. Examples 17 OS env variable export SPRING_PROFILES_ACTIVE=development export SERVER_PORT=8090 gradle bootRun java -­‐jar build/libs/demo-­‐1.0.0.jar with a -­‐D argument (remember to put it before the main class or jar archive) java -­‐jar -­‐Dspring.profiles.active=development build/libs/dem-­‐1.0.0.jar java -­‐jar -­‐Dserver.port=8090 build./libs/demo-­‐1.0.0.jar
  • 18. Using Spring data • Add dependency compile 'org.springframework.boot:spring-­‐boot-­‐starter-­‐data-­‐mongodb' • Configure database URL spring.data.mongodb.uri=mongodb://localhost/springtestdev • Add entity class import org.springframework.data.annotation.Id; class • Add Person{@Id String id, String name} repository interface import org.springframework.data.mongodb.repository.MongoRepository public interface PersonRepository extends • Autowire and use like charm MongoRepository<Person, String> {} @Autowired PersonRepository personRepository personRepository.save(new Person(name:'Spring Boot')) personRepository.findAll() personRepository.count()
  • 19. Next level persistence with GORM • Add dependencies to use GORM-­‐Hibernate • For GORM MongoDB use the following dependencies compile 'org.grails:gorm-­‐mongodb-­‐spring-­‐boot:1.1.0.RELEASE' • Add entity with @grails.persistence.entity 19 compile 'org.springframework.boot:spring-­‐boot-­‐starter-­‐data-­‐jpa' compile 'org.grails:gorm-­‐hibernate4-­‐spring-­‐boot:1.1.0.RELEASE' runtime 'com.h2database:h2' //for h2 database import grails.persistence.Entity @Entity class Person { String name; Integer age static constraints = { name blank: false age min: 15 } } Further reading https://github.com/grails/grails-­‐data-­‐mapping
  • 20. Server side view template libraries • JSP/JSTL • Thymeleaf • Freemarker • Velocity • Tiles • GSP • Groovy Template Engine 20
  • 21. • Very limited existing tags available • https://github.com/grails/grails-­‐boot/issues/3 • Add dependency • Put GSP templates in resources/templates folder GSP 21 compile "org.grails:grails-­‐gsp-­‐spring-­‐boot:1.0.0.RC1" compile "org.grails:grails-­‐web:2.4.0.M2"
  • 22. GSP continued... • Sample request handler 22 @RequestMapping("/show/{id}") public ModelAndView show(@PathVariable Long id) { Person person = Person.read(id) if (person) { //render(view:"/person/show", model:[personInstance:personInstance]) new ModelAndView("/person/show", [personInstance: Person.get(id)]) } else { log.info "No entity fount for id : " + id //redirect(controller:"person", action:"list") new ModelAndView("redirect:/person/list") } }
  • 23. Grails Taglib 23 @grails.gsp.TagLib @org.springframework.stereotype.Component class ApplicationTagLib { static namespace = "app" def paginate = { attrs -­‐> String action = attrs.action Integer total = attrs.total Integer currentPage = attrs.currentPage ?: 1 Integer pages = (total / 10) + 1 out << render(template: "/shared/pagination", model: [action: action, total: total, currentPage: currentPage, pages: pages] ) } } <app:paginate total="${personInstanceCount ?: 0}" currentPage="${currentPage}" action="/person/list"/>
  • 24. Packaging executable jar and war files 24 Packaging as jar with embedded tomcat $ gradle build $ java -­‐jar build/libs/mymodule-­‐0.0.1-­‐SNAPSHOT.jar Packaging as war : configure build.groovy //... apply plugin: 'war' war { baseName = 'myapp' version = '0.5.0' } //.... configurations { providedRuntime } dependencies { compile("org.springframework.boot:spring-­‐boot-­‐starter-­‐web") providedRuntime("org.springframework.boot:spring-­‐boot-­‐starter-­‐tomcat") // ... } $ gradle war
  • 26. Thank you. 26 Blog: http://www.intelligrape.com/blog/author/bhagwat LikedIn: http://www.linkedin.com/in/bhagwatkumar Twitter: http://twitter.com/bhagwatkumar Mail : [email protected]
  • 27. References Samples : https://github.com/bhagwat/spring-­‐boot-­‐samples http://docs.spring.io/spring-­‐boot/docs/current-­‐SNAPSHOT/reference/htmlsingle http://docs.spring.io/spring-­‐boot/docs/current-­‐SNAPSHOT/reference/htmlsingle/#getting-­‐ started-­‐gvm-­‐cli-­‐installation https://github.com/spring-­‐projects/spring-­‐boot/tree/master/spring-­‐boot-­‐cli/samples http://docs.spring.io/spring-­‐boot/docs/current-­‐SNAPSHOT/reference/htmlsingle/#using-­‐boot-­‐ starter-­‐poms http://spring.io/guides/gs/accessing-­‐mongodb-­‐data-­‐rest/ https://spring.io/guides/gs/accessing-­‐data-­‐mongodb/ https://spring.io/guides/gs/accessing-­‐data-­‐jpa/ http://www.gradle.org/ http://www.slideshare.net/Soddino/developing-­‐an-­‐application-­‐with-­‐spring-­‐boot-­‐34661781 http://presos.dsyer.com/decks/spring-­‐boot-­‐intro.html http://pygments.org/ for nicely formatting code snippets included in presentation 27