Download - Spring boot
![Page 1: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/1.jpg)
Code Camp April-Spring BootApril 2, 2016 Presented By - Nakul & Vishal
![Page 2: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/2.jpg)
AudienceBeginner Level
Java Web Developers
Grails Developers
![Page 3: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/3.jpg)
Objective
To get familiar with the Spring Boot framework and use it to build microservices architecture.
![Page 4: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/4.jpg)
Agenda
Introducing Spring Boot
Features
Artifacts
Profiling
Demo application using Spring Boot and Groovy Templates
![Page 5: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/5.jpg)
Introducing Spring Boot - If Spring is the cake, Spring
Boot is the icing.
![Page 6: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/6.jpg)
Spring boot is a suite, pre-configured, pre-sugared set of frameworks/technologies to reduce boilerplate configuration providing you the shortest way to have a Spring web application up and running with smallest line of code/configuration out-of-the-box.
What is Spring Boot ?
![Page 7: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/7.jpg)
The primary goals of Spring Boot are:
To provide a radically faster and widely accessible 'getting started' experience for all Spring development
To be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults
To provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration)
Spring Boot does not generate code and there is absolutely no requirement for XML configuration.
Why Spring Boot ?
![Page 8: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/8.jpg)
import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;
@Controller@EnableAutoConfiguration
@Configuration
@ComponentScan
//All three annotations can be replaced by one @SpringBootApplicationpublic class SampleController { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); }}
A simple Spring Boot Application
![Page 9: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/9.jpg)
Demo
Let’s hit some keys !!
Hello World from Spring Boot
![Page 10: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/10.jpg)
❖@Controller - annotates a class as an MVC controller
❖@Configuration - tags the class as a source of bean definitions for the application context.
❖@EnableAutoConfiguration - enables auto configuration for the application
❖@ComponentScan - This tells Spring to look for classes with @Component ,@Configuration , @Repository ,
@Service , and @Controller and wire them into the app context as beans.
❖@SpringBootApplication - get the functionality of all above annotations in a single annotation
❖@RequestMapping - maps the incoming request uri to a particular method
No XML configuration needs to be done.
Whats Happening?
![Page 11: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/11.jpg)
How Spring boot works ?
![Page 12: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/12.jpg)
Directory Structure of Spring Boot
There is no restrictions on directory structure.
We can create one as we are used to in grails or we can create one that is shown in the picture.
![Page 13: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/13.jpg)
Demo
Let’s hit some keys !!
Look at the directory structure
![Page 14: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/14.jpg)
Spring Boot Essentials
Spring Boot brings a great deal of magic to Spring application development.
But there are four core tricks that it performs:
■ Automatic configuration — Spring Boot can automatically provide configuration for application functionality common to many Spring applications.
■ Starter dependencies — You tell Spring Boot what kind of functionality you need,and it will ensure that the libraries needed are added to the build.
■ The command-line interface — This optional feature of Spring Boot lets you write complete applications with just application code, but no need for a traditional project build.
■ The Actuator — Gives you insight into what’s going on inside of a running Spring Boot application.
![Page 15: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/15.jpg)
What Spring Boot isn’t ?
1. Spring Boot is not an application server. It has embedded server that helps it run by executing a jar file of the project.
2. Spring Boot doesn’t implement any enterprise Java specifications such as JPA or JMS. It auto configures these beans and provide them at runtime to our disposal.
3. Spring Boot doesn’t employ any form of code generation to accomplish its
magic. Instead, it leverages conditional configuration features from Spring, along with transitive dependency resolution offered by Maven and Gradle,
to automatically configure beans in the Spring application context.
![Page 16: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/16.jpg)
Basic Artifacts
Dependency Resolution
Domains
Repositories
Controllers
Services
Views
Configuration
Deployment
![Page 17: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/17.jpg)
1. Done with the help of Gradle or Maven.
2. Dependencies are resolved by build.gradle in gradle build environment.
3. For maven they are resolved from pom.xml
4. Gradle supports groovy DSL like syntax and is much easier to maintain. It frees us from the fuss of writing xml for dependency resolution.
Dependency Resolution
![Page 18: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/18.jpg)
Resolving dependencies using Gradle
Let’s have a look at the build.gradle file in the root of our application.
![Page 19: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/19.jpg)
Demo
Let’s hit some keys !!
build.gradle
![Page 20: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/20.jpg)
1. Annotated by the annotation @Entity
2. javax.persistence.Entity/grails.persistence.Entity/org.hibernate.annotations.Entity
3. Can use JPA 2.1, Hibernate, Spring Data, GORM etc .
4. Classes annotated by @Entity is a component which our main class searches for by the help of @ComponentScan
Domains
![Page 21: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/21.jpg)
Demo
Let’s hit some keys !!
Domains
![Page 22: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/22.jpg)
1. Provides abstraction.
2. Significantly reduces the amount of boilerplate code required to implement DAO layer
3. Supports various persistence stores like MySql , MongoDB etc.
The central interface in Spring Data repository abstraction is Repository.It takes the the domain class to manage as well as the id type of the domain class as type arguments.
This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one.
Repositories
![Page 23: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/23.jpg)
The CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed.
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { S save(S entity); //Saves the given entity
T findOne(ID primaryKey); //Returns the entity identified by the given id
Iterable<T> findAll(); //Returns all entities
Long count(); //Returns the number of entities
void delete(T entity); //Deletes the given entity
boolean exists(ID primaryKey); //Indicates whether an entity with the given id exists
// … more functionality omitted.}
![Page 24: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/24.jpg)
How a repository works ?
1. Declare an interface extending Repository or one of its subinterfaces and type it to the domain class that it will handle.
public interface PersonRepository extends Repository<User, Long> { … }
2. Declare query methods on the interface.
List<Person> findByLastname(String lastname);
3. Get the repository instance injected and use it.
public class SomeClient {
@Autowired private PersonRepository repository;
public void doSomething() { List<Person> persons = repository.findByLastname("Nakul"); }
}
![Page 25: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/25.jpg)
Demo
Let’s hit some keys !!
Repositories
![Page 26: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/26.jpg)
1. Any java class can be converted into a controller by annotating it with @Controller or @RestController
2. @Controller - makes a java/groovy class as an MVC controller that renders a view
3. @RestController - makes a java/groovy class as a rest controller.
Controllers
![Page 27: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/27.jpg)
What about ‘actions’ ?
Any method defined inside a class annotated by @Controller or @RestController will behave like an action only if it has been annotated by @RequestMapping
Ex - @RestController@RequestMapping(value = '/student')class RootController { @Autowired StudentRepository studentRepository @RequestMapping(method=RequestMethod.GET ,value = '{id}',produces = MediaType.APPLICATION_JSON_VALUE) public Student getOne(@PathVariable String id) { return studentRepository.findOne(Long.parseLong(id))}}
![Page 28: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/28.jpg)
What about ‘actions’ ? Continued -
A simple MVC controller
Ex - @Controller@RequestMapping(value = '/student')class RootController { @Autowired StudentRepository studentRepository @RequestMapping(method=RequestMethod.GET ,value = '{id}') public ModelAndView getOne(@PathVariable String id) { return new ModelAndView(“views/index”,[:])}}
![Page 29: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/29.jpg)
Demo
Let’s hit some keys !!
Controllers and Actions
![Page 30: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/30.jpg)
1. Any java/groovy class can be converted into a service by annotating it with @Service
2. To make a service transactional mark it with @Transactional
3. @Transactional(propagation = REQUIRED) makes a transaction complete in itself
4. @Transactional(propagation = MANDATORY) is used to
protect the other methods from being called erroneously out of a transaction
5. @Transactional(readOnly=true) makes a service read-only.
Services
![Page 31: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/31.jpg)
Demo
Let’s hit some keys !!
Services
![Page 32: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/32.jpg)
1. Spring supports Groovy template engine natively to render views modelled by the controller.
2. Thymeleaf is also a popular rendering engine being used with spring-boot
3. GSP’s can also be used as a part of presentation layer.
4. We can also use Angular.js as a frontend framework for use with REST-API’s for make a complete web-app.
Views
![Page 33: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/33.jpg)
From where can we get these rendering engines ?
Can be maintained as gradle dependencies in build.gradle
Dependencies
Groovy Template Engine - compile ("org.codehaus.groovy:groovy-templates:2.4.0")
Thymeleaf Engine - compile("org.springframework.boot:spring-boot-starter-thymeleaf")
Groovy Server Pages - compile "org.grails:grails-web-gsp:2.5.0" compile "org.grails:grails-web-gsp-taglib:2.5.0" provided "org.grails:grails-web-jsp:2.5.0"
![Page 34: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/34.jpg)
Groovy Template Engine
Features
1. hierarchical (builder) syntax to generate XML-like contents (in particular, HTML5)
2. template includes3. compilation of templates to bytecode for fast rendering4. layout mechanism for sharing structural patterns
![Page 35: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/35.jpg)
Groovy Template Engine
Dependency
dependencies { compile "org.codehaus.groovy:groovy" compile "org.codehaus.groovy:groovy-templates" }
![Page 36: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/36.jpg)
Groovy Template Engine
Example 1
link(rel: 'stylesheet', href: '/css/bootstrap.min.css')
will be rendered as:
<link rel='stylesheet' href='/css/bootstrap.min.css'/>
![Page 37: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/37.jpg)
Groovy Template Engine
Example 2a(class: 'brand',
href: 'http://beta.groovy-lang.org/docs/groovy-2.3.2/html/documentation/markup-template-engine.html', 'Groovy - Template Engine docs')
will be rendered as:
<a class='brand' href='http://beta.groovy-lang.org/docs/groovy-2.3.2/html/documentation/markup-template-engine.html'>Groovy - Template Engine docs</a>
![Page 38: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/38.jpg)
Groovy Template Engine
Example 3a(class: 'brand', href:‘
http://beta.groovy-lang.org/docs/groovy-2.3.2/html/documentation/markup-template-engine.html'){
yield 'Groovy - Template Engine docs'
}
will be rendered as:
<a class='brand' href='http://beta.groovy-lang.org/docs/groovy-2.3.2/html/documentation/markup-template-engine.html'>Groovy - Template Engine docs</a>
![Page 39: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/39.jpg)
Demo
Let’s hit some keys !!
Views
![Page 40: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/40.jpg)
Groovy Template Engine - Layouts
Layouts-Example
yieldUnescaped '<!DOCTYPE html>'html { head { title(pageTitle) link(rel: 'stylesheet', href: '/css/bootstrap.min.css') } body { a(class: 'brand', href: 'http://beta.groovy-lang.org/docs/groovy-2.3.2/html/documentation/markup-template-engine.html', 'Groovy - Template Engine docs')
![Page 41: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/41.jpg)
Groovy Template Engine - Layouts
Layouts - Example
a(class: 'brand', href: 'hhttp://projects.spring.io/spring-boot/') { yield 'Spring Boot docs' mainBody() } }}
![Page 42: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/42.jpg)
Groovy Template Engine - Layouts
Layouts
1. Common part of our template is kept into a main.tpl file that we will save into src/main/resources/templates/layouts
2. title(pageTitle) where pageTitle is expected to be the page title
that we want to give
3. mainBody(), which will cause rendering of the main body for
pages using that layout.
![Page 43: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/43.jpg)
Groovy Template Engine - Layouts
Layouts in action
layout 'layouts/main.tpl', pageTitle: 'Spring Boot - Groovy templates example with layout', mainBody: contents { div("This is an application using Spring Boot and Groovy Template Engine") }
![Page 44: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/44.jpg)
Groovy Template Engine - Layouts
Layouts
we call the layout method and provide it with several arguments:
the name of the layout file to be used (layouts/main.tpl)
pageTitle, a simple string
mainBody, using the contents block
![Page 45: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/45.jpg)
Groovy Template Engine - Layouts
Layouts
Use of the contents block will trigger the rendering of the contents of mainBody inside the layout when the mainBody() instruction is found. So using this layout file, we are definitely sharing a common, structural pattern, against multiple templates.
layouts are themselves composable, so you can use layouts inside layouts…
![Page 46: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/46.jpg)
Demo
Let’s hit some keys !!
Layouts
![Page 47: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/47.jpg)
Profiles
Profiles
In the normal Spring way, you can use a spring.profiles.active Environment property to specify which profiles are active.
specify on the command line using the switch
--spring.profiles.active=dev
--spring.profiles.active=test
--spring.profiles.active=production
For every environment we create a application-{environment}.properties files in the resource directory.
![Page 48: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/48.jpg)
application.properties
application.properties
If no environment is specified then spring boot picks the default application.properties file from the ‘resources’ directory.
Stores all the configurations in a single file .
All the configurable properties can be referenced here
![Page 49: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/49.jpg)
Demo
Let’s hit some keys !!
Profiles
![Page 50: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/50.jpg)
References
References
Groovy Templates : https://spring.io/blog/2014/05/28/using-the-innovative-groovy-template-engine-in-spring-boot
Spring Boot : Spring Boot in Action - Craig Walls, Manning Publication
Learning Spring Boot - Greg L. Turnquist, Packt Publishing
More References - http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
![Page 51: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/51.jpg)
Questions ?
![Page 52: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/52.jpg)
For dummy project please visit https://github.com/pant-nakul/springboot-crud-demo
![Page 53: Spring boot](https://reader035.vdocuments.us/reader035/viewer/2022062822/587deec41a28abab7e8b47d1/html5/thumbnails/53.jpg)
Thank You for your patience !!!