Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Grails Spring Boot


Published on

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.

Published in: Technology
  • You might get some help from ⇒ ⇐ Success and best regards!
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Grails Spring Boot

  1. 1. Spring Boot By Bhagwat Kumar
  2. 2. 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
  3. 3. • 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?
  4. 4. • 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
  5. 5. • 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
  6. 6. @Controller classExample{ @RequestMapping("/") @ResponseBody public StringhelloWorld(){ "Hello Spring bootaudience!!!" } } GETTING STARTED REALLY QUICKLY
  7. 7. // importorg.springframework.web.bind.annotation.Controller // otherimports... //@Grab("org.springframework.boot:spring-boot-web-starter:0.5.0") //@EnableAutoConfiguration @Controller classExample{ @RequestMapping("/") @ResponseBody publicStringhello(){ return"HelloWorld!"; } // public static void main(String[]args){ //,args); // } } WHAT JUST HAPPENED?
  8. 8. • 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
  11. 11. task hello <<{ println "Hello!!!!" } task greet<<{ println "Welocome Mr.Kumar" } task intro(dependsOn: hello)<<{ println "I'mGradle" } hello <<{println "Hello extended!!!!" } greet.dependsOn hello,intro // gradletasks // gradleintro :listall the available tasks :executes introtask // gradle -q greet :bare build output // gradle --daemon hello:subsequent execution wil be fast BUILD.GRADLE
  12. 12. apply plugin:"groovy" //look for sources in src/main/groovyfolder //inherits java plugin: src/main/javafolder // 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: USING PLUGIN AND ADDING DEPENDENCIES
  13. 13. 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' } BUILD.GRADLE: FOR SPRING BOOT APP WITH HOT RELOADING
  14. 14. • Put somewhere in classpath • Easy one: src/main/resourcesfolder ENVIRONMENT AND PROFILE AKA GRAILS CONFIG
  15. 15. Using ConfigurationProperties annotation import 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('${}') StringappName @Value('${server.port}') Integerport } BINDING PROPERTIES
  16. 16. 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 build/libs/dem-1.0.0.jar java -jar -Dserver.port=8090build./libs/demo-1.0.0.jar EXAMPLES
  17. 17. • Add dependency compile 'org.springframework.boot:spring-boot-starter-data-mongodb' • Configure database URL • Add entityclass; classPerson{@IdStringid, Stringname} • Add repositoryinterface public interface PersonRepositoryextendsMongoRepository<Person, String> {} • Autowire and use like charm @AutowiredPersonRepository personRepository Person(name:'SpringBoot')) personRepository.findAll() personRepository.count() USING SPRING DATA
  18. 18. • 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
  19. 19. • JSP/JSTL • Thymeleaf • Freemarker • Velocity • Tiles • GSP • Groovy TemplateEngine SERVER SIDE VIEW TEMPLATE LIBRARIES
  20. 20. • Very limited existing tags available • 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
  21. 21. • Sample requesthandler @RequestMapping("/show/{id}") public ModelAndView show(@PathVariableLong id){ Person person if(person) { new ModelAndView("/person/show",[personInstance:Person.get(id)]) }else{ "No entity fount for id:"+id newModelAndView("redirect:/person/list") } } GSP CONTINUED
  22. 22. @grails.gsp.TagLib @org.springframework.stereotype.Component class ApplicationTagLib{ static namespace ="app" def paginate ={attrs -> String action =attrs.action Integer 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"/> GRAILS TAGLIB
  23. 23. 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
  24. 24. 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!