This tutorial is about Spring Boot.
The tutorial includes an introduction to Spring Boot, key features of Spring Boot, prototyping using CLI, managing profiles aka environment in Grails, using GORM and using GSP. The tutorial begins with a section which is an introduction to Spring Boot. It includes an introduction to Spring Boot, the benefits of using Spring Boot.
Following is a features section which includes the key features of Spring Boot like embedded servers, security, metrics etc. Next is a section about prototyping. It includes prototyping using CLI, getting started, the things that happen during prototyping, starter POMs and a demo, building Gradle, using plugin and adding dependencies, and hot reloading.
Consecutively there is a section about managing profiles. It includes managing profiles aka managing environment in Grails like binding properties and its examples, using spring data to add dependency. Moreover, there's also a section which includes using GORM for next level persistence and also includes server side view template libraries like JSP, velocity, tiles, GSP etc.
The last section of this tutorial is about GSP. It includes using GSP with Spring Boot as it has limited tags, adds dependency and helps in packaging executable JAR and WAR files.
3. AGENDA
1. What and why?
2. Key features ofSpring boot
3. Prototyping usingCLI
4. Gradle primer
5. Managing profiles aka environment in grails
6. Using Spring data libraries e.g. MongoDB
7. UsingGORM
8. Presentation layer
9. UsingGSP
4. • Its not a replacement for Spring framework but it presents a small
surface area for usersto 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.0will be based on Spring Boot
WHAT & WHY?
5. • Stand-alone Springapplications
• No code generation/ No XML Config
• Automatic configuration by creating sensible defaults
• Starterdependencies
• Structure your code as you like
• Supports Gradle andMaven
• Common non-functional requirements for a "real" application
- embedded servers,
- security, metrics, healthchecks
- externalized configuration
KEY FEATURES
6. • Quickest way to get a spring app off the ground
• Allows you to run groovy scripts without much boilerplate code
• Not recommended forproduction
RAPID PROTOTYPING : SPRING CLI
9. • 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 runningquickly
• All starters follow a similarnaming 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
STARTER POMs
16. Using ConfigurationProperties annotation
import org.springframework.boot.context.properties.ConfigurationProperties
importorg.springframework.stereotype.Component
@Component
@ConfigurationProperties(prefix="app")
class AppInfo {
String name
Stringversion
}
Using Valueannotation
importorg.springframework.beans.factory.annotation.Value
importorg.springframework.stereotype.Component
@Component
classAppConfig{
@Value('${app.name}')
StringappName
@Value('${server.port}')
Integerport
}
BINDING PROPERTIES
17. OS envvariable
export SPRING_PROFILES_ACTIVE=development
export SERVER_PORT=8090
gradle bootRun
java -jarbuild/libs/demo-1.0.0.jar
with a -D argument (remember to put it before the main class or jararchive)
java -jar -Dspring.profiles.active=development build/libs/dem-1.0.0.jar
java -jar -Dserver.port=8090build./libs/demo-1.0.0.jar
EXAMPLES
18. • Add dependency
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
• Configure database URL
spring.data.mongodb.uri=mongodb://localhost/springtestdev
• Add entityclass
importorg.springframework.data.annotation.Id;
classPerson{@IdStringid, Stringname}
• Add repositoryinterface
importorg.springframework.data.mongodb.repository.MongoRepository
public interface PersonRepositoryextendsMongoRepository<Person, String> {}
• Autowire and use like charm
@AutowiredPersonRepository personRepository
personRepository.save(new Person(name:'SpringBoot'))
personRepository.findAll()
personRepository.count()
USING SPRING DATA
19. • Add dependencies to use GORM-Hibernate
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 h2database
• Add entity with@grails.persistence.entity
importgrails.persistence.Entity
@Entity
class Person{
Stringname;
Integerage
static constraints ={
name blank:false
age min:15
}
}
• For GORM MongoDB use the following dependencies
compile 'org.grails:gorm-mongodb-spring-boot:1.1.0.RELEASE'
NEXT LEVEL PERSISTENCE WITH GORM
21. • Very limited existing tags available
https://github.com/grails/grails-boot/issues/3
• Add dependency
compile "org.grails:grails-gsp-spring-boot:1.0.0.RC1"
compile "org.grails:grails-web:2.4.0.M2"
• Put gsptemplates in resources/templates folder
GSP
22. • Sample requesthandler
@RequestMapping("/show/{id}")
public ModelAndView show(@PathVariableLong id){
Person person =Person.read(id)
if(person) {
new ModelAndView("/person/show",[personInstance:Person.get(id)])
}else{
log.info "No entity fount for id:"+id
newModelAndView("redirect:/person/list")
}
}
GSP CONTINUED
24. Packaging as jar with embedded tomcat
$gradle build
$java -jarbuild/libs/mymodule-0.0.1-SNAPSHOT.jar
Packaging as war :configurebuild.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
PACKAGING EXECUTABLE JAR AND WAR
FILES
25. Contact us
Our Office
Client
Location
Click Here To Know More!
Have more queries on Grails?
Talk to our GRAILS experts
Now!
Talk To Our Experts
Here's how the world's
biggest Grails team is
building enterprise
applications on Grails!