KEMBAR78
Spring Boot Introduction and framework.ppt
Spring Boot Introduction
Spring MVC, Spring Data
Spring Boot
Introduction
Table of Contents
1. Spring Boot Components
2. Spring MVC
3. Spring Data
2
3
sli.do
#JavaWeb
Have a Question?
4
What is Spring Boot?
5
 Opinionated view of building production-ready Spring
applications
Spring Boot
Spring Boot
Tomcat
pom.xml
Auto configuration
6
Spring Boot
Spring Boot
7
 Just go to https://start.spring.io/
Creating Spring Boot Project
8
 Additional set of tools that can make the application
development faster and more enjoyable
Spring Dev Tools
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
pom.xml
9
Spring Dev Tools Setup
10
Spring Resources
HTML, CSS, JS
Thymeleaf
App propertues
11
 Four main components:
 Spring Boot Starters - combine a group of common or related
dependencies into single dependency
 Spring Boot AutoConfigurator - reduce the Spring Configuration
 Spring Boot CLI - run and test Spring Boot
applications from command prompt
 Spring Boot Actuator – provides EndPoints and
Metrics
Spring Boot Main Components
12
Spring Boot Starters (1)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
pom.xml
13
Spring Boot Starters (2)
spring-boot-starter-web
spring-web spring-web-mvc
spring-boot-starter
spring-boot-starter-tomcat
tomcat-embed-core
tomcat-embed-logging-juli
spring-boot
spring-boot-autoconfigure
spring-boot-starter-logging
14
Spring Boot AutoConfigurator
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
)}
)
public @interface SpringBootApplication
pom.xml
15
 Command Line Interface - Spring Boot software to run and test
Spring Boot applications
Spring Boot CLI
16
 Expose different types of information about the running
application
Spring Boot Actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
pom.xml
17
 Spring provides Inversion of Control and Dependency Injection
Inversion of Control
//Tradiotional Way
public class UserServiceImpl
implements UserService {
private UserRepository
userRepository = new
UserRepository();
}
UserServiceImpl.java
//Dependency Injection
@Service
public class UserServiceImpl
implements UserService {
@Autowired
private UserRepository
userRepository;
}
UserServiceImpl.java
18
Spring IoC
Meta Data:
1. XML Config
2. Java Config
3. Annotation Config
Automatic Beans:
1. @Component
2. @Service
3. @Repository
Fully Configured System
Explicit Beans
1. @Bean
IoC
19
 Object that is instantiated, assembled, and otherwise managed
by a Spring IoC container
Beans
public class Dog implements Animal {
private String name;
public Dog() {}
//GETTERS AND SETTERS
}
Dog.java
20
Bean Declaration
@SpringBootApplication
public class MainApplication {
…
@Bean
public Animal getDog(){
return new Dog();
}
}
Dog.java
Bean Declaration
21
Get Bean from Application Context
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(MainApplication.class, args);
Animal dog = context.getBean(Dog.class);
System.out.println("DOG: " +
dog.getClass().getSimpleName());
}
}
MainApplication.java
22
Bean Lifecycle
Instantiation Set Properties Set Name
Set Application
Context
Pre Initialization
Initialization
Post Initialization Bean is ready Bean is destroyed
When Container
is Shutdown
23
Bean Lifecycle Demo (1)
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(MainApplication.class, args);
((AbstractApplicationContext)context).close();
}
@Bean(destroyMethod = "destroy", initMethod =
"init")
public Animal getDog(){
return new Dog();
MainApplication.java
24
Bean Lifecycle Demo (2)
public class Dog implements Animal {
public Dog() {
System.out.println("Instantiation");
}
public void init(){
System.out.println("Initializing..");
}
public void destroy(){
System.out.println("Destroying..");
}
}
MainApplication.java
25
 The default one is Singleton. It is easy to change to Prototype
Bean Scope
Request A
Request B
Request C
Request A
Request B
Request C
Dog
Dog 1
Dog 2
Dog 3
Singleton Prototype
Mostly used
as State-full
Mostly used
as State-less
26
Bean Scope Demo
27
What is Spring MVC?
28
Execute
Action
 Model-view-controller (MVC) framework is designed around a
DispatcherServlet that dispatches requests to handlers
What is Spring MVC?
Dispatcher
Servlet
Handler
Mapping
Handler
Adapter
View
Resolver
Controller
View Name
Model
View
Service
Repository
DB
Request
Find
Controller
Resolve
View
Result
Model
Result
Model
Business Logic
Response
29
Response
(html, json, xml)
MVC – Control Flow
Web Client
Model
Controller
View
Request
User Action
Update
Model
Notify
Update
View
30
Controllers
@Controller
public class DogController {
@GetMapping("/dog")
@ResponseBody
public String getDogHomePage(){
return "I am a dog page";
}
}
DogController.java
Controller
Request Mapping
Action
Text
Print Text
31
Actions – Get Requests
31
@Controller
public class CatController {
@GetMapping("/cat")
public String getHomeCatPage(){
return "cat-page.html";
}
}
CatController.java
Request Mapping
Action
View
32
Actions – Post Requests (1)
@Controller
@RequestMapping("/cat")
public class CatController {
@GetMapping("")
public String getHomeCatPage(){
return "new-cat.html";
}
}
CatController.java
Starting route
33
Actions – Post Requests (1)
@Controller
@RequestMapping("/cat")
public class CatController {
@PostMapping("")
public String addCat(@RequestParam String catName,
@RequestParam int catAge){
System.out.println(String.format("Cat Name: %s,
Cat Age: %d", catName, catAge));
return "redirect:/cat";
}
}
CatController.java
Request param
Redirect
34
Models and Views
@Controller
public class DogController {
@GetMapping("/dog")
public ModelAndView getDogHomePage(ModelAndView
modelAndView){
modelAndView.setViewName("dog-page.html");
return modelAndView;
}
}
DogController.java
Model and View
35
Path Variables
@Controller
@RequestMapping("/cat")
public class CatController {
@GetMapping("/edit/{catId}")
@ResponseBody
public String editCat(@PathVariable long catId){
return String.valueOf(catId);
}
}
CatController.java
Path Variable
36
Spring MVC Demo
37
Spring Data
38
Overall Architecture
Database
Repository Service
Models/
DTO
Controller
Entities
View
Back-end
39
Application Properties
#Data Source Properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cat_store?
useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=1234
#JPA Properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect
.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.format_sql=TRUE
spring.jpa.hibernate.ddl-auto=update
application.properties
40
 Entity is a lightweight persistence domain object
Entities
@Entity
@Table(name = "cats")
public class Cat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
//GETTERS AND SETTERS
}
Cat.java
41
 Persistence layer that works with entities
Repositories
@Repository
public interface CatRepository extends CrudRepository<Cat, Long>
{
}
CatRepository.java
42
 Business Layer. All the business logic is here.
Services
@Service
public class CatServiceImpl implements CatService {
@Autowired
private CatRepository catRepository;
@Override
public void buyCat(CatModel catModel) {
//TODO Implement the method
}
}
CatService.java
43
Spring Data Demo
44
 Spring Boot - Opinionated view of building
production-ready Spring applications
 Spring MVC - MVC framework that has three
main components:
 Controller - controls the application flow
 View - presentation layer
 Model - data component with the main logic
 Spring Data - Responsible for database related
operations
Summary
?
?
?
?
?
?
?
?
?
Questions?
Web Development Basics – Course Overview
https://softuni.bg/courses/
License
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International"
license
46
Free Trainings @ Software University
 Software University Foundation – softuni.org
 Software University – High-Quality Education,
Profession and Job for Software Developers
 softuni.bg
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University @ YouTube
 youtube.com/SoftwareUniversity
 Software University Forums – forum.softuni.bg

Spring Boot Introduction and framework.ppt

  • 1.
    Spring Boot Introduction SpringMVC, Spring Data Spring Boot Introduction
  • 2.
    Table of Contents 1.Spring Boot Components 2. Spring MVC 3. Spring Data 2
  • 3.
  • 4.
  • 5.
    5  Opinionated viewof building production-ready Spring applications Spring Boot Spring Boot Tomcat pom.xml Auto configuration
  • 6.
  • 7.
    7  Just goto https://start.spring.io/ Creating Spring Boot Project
  • 8.
    8  Additional setof tools that can make the application development faster and more enjoyable Spring Dev Tools <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> pom.xml
  • 9.
  • 10.
    10 Spring Resources HTML, CSS,JS Thymeleaf App propertues
  • 11.
    11  Four maincomponents:  Spring Boot Starters - combine a group of common or related dependencies into single dependency  Spring Boot AutoConfigurator - reduce the Spring Configuration  Spring Boot CLI - run and test Spring Boot applications from command prompt  Spring Boot Actuator – provides EndPoints and Metrics Spring Boot Main Components
  • 12.
    12 Spring Boot Starters(1) <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.4.1.RELEASE</version> </dependency> pom.xml
  • 13.
    13 Spring Boot Starters(2) spring-boot-starter-web spring-web spring-web-mvc spring-boot-starter spring-boot-starter-tomcat tomcat-embed-core tomcat-embed-logging-juli spring-boot spring-boot-autoconfigure spring-boot-starter-logging
  • 14.
  • 15.
    15  Command LineInterface - Spring Boot software to run and test Spring Boot applications Spring Boot CLI
  • 16.
    16  Expose differenttypes of information about the running application Spring Boot Actuator <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> pom.xml
  • 17.
    17  Spring providesInversion of Control and Dependency Injection Inversion of Control //Tradiotional Way public class UserServiceImpl implements UserService { private UserRepository userRepository = new UserRepository(); } UserServiceImpl.java //Dependency Injection @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; } UserServiceImpl.java
  • 18.
    18 Spring IoC Meta Data: 1.XML Config 2. Java Config 3. Annotation Config Automatic Beans: 1. @Component 2. @Service 3. @Repository Fully Configured System Explicit Beans 1. @Bean IoC
  • 19.
    19  Object thatis instantiated, assembled, and otherwise managed by a Spring IoC container Beans public class Dog implements Animal { private String name; public Dog() {} //GETTERS AND SETTERS } Dog.java
  • 20.
    20 Bean Declaration @SpringBootApplication public classMainApplication { … @Bean public Animal getDog(){ return new Dog(); } } Dog.java Bean Declaration
  • 21.
    21 Get Bean fromApplication Context @SpringBootApplication public class MainApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(MainApplication.class, args); Animal dog = context.getBean(Dog.class); System.out.println("DOG: " + dog.getClass().getSimpleName()); } } MainApplication.java
  • 22.
    22 Bean Lifecycle Instantiation SetProperties Set Name Set Application Context Pre Initialization Initialization Post Initialization Bean is ready Bean is destroyed When Container is Shutdown
  • 23.
    23 Bean Lifecycle Demo(1) @SpringBootApplication public class MainApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(MainApplication.class, args); ((AbstractApplicationContext)context).close(); } @Bean(destroyMethod = "destroy", initMethod = "init") public Animal getDog(){ return new Dog(); MainApplication.java
  • 24.
    24 Bean Lifecycle Demo(2) public class Dog implements Animal { public Dog() { System.out.println("Instantiation"); } public void init(){ System.out.println("Initializing.."); } public void destroy(){ System.out.println("Destroying.."); } } MainApplication.java
  • 25.
    25  The defaultone is Singleton. It is easy to change to Prototype Bean Scope Request A Request B Request C Request A Request B Request C Dog Dog 1 Dog 2 Dog 3 Singleton Prototype Mostly used as State-full Mostly used as State-less
  • 26.
  • 27.
  • 28.
    28 Execute Action  Model-view-controller (MVC)framework is designed around a DispatcherServlet that dispatches requests to handlers What is Spring MVC? Dispatcher Servlet Handler Mapping Handler Adapter View Resolver Controller View Name Model View Service Repository DB Request Find Controller Resolve View Result Model Result Model Business Logic Response
  • 29.
    29 Response (html, json, xml) MVC– Control Flow Web Client Model Controller View Request User Action Update Model Notify Update View
  • 30.
    30 Controllers @Controller public class DogController{ @GetMapping("/dog") @ResponseBody public String getDogHomePage(){ return "I am a dog page"; } } DogController.java Controller Request Mapping Action Text Print Text
  • 31.
    31 Actions – GetRequests 31 @Controller public class CatController { @GetMapping("/cat") public String getHomeCatPage(){ return "cat-page.html"; } } CatController.java Request Mapping Action View
  • 32.
    32 Actions – PostRequests (1) @Controller @RequestMapping("/cat") public class CatController { @GetMapping("") public String getHomeCatPage(){ return "new-cat.html"; } } CatController.java Starting route
  • 33.
    33 Actions – PostRequests (1) @Controller @RequestMapping("/cat") public class CatController { @PostMapping("") public String addCat(@RequestParam String catName, @RequestParam int catAge){ System.out.println(String.format("Cat Name: %s, Cat Age: %d", catName, catAge)); return "redirect:/cat"; } } CatController.java Request param Redirect
  • 34.
    34 Models and Views @Controller publicclass DogController { @GetMapping("/dog") public ModelAndView getDogHomePage(ModelAndView modelAndView){ modelAndView.setViewName("dog-page.html"); return modelAndView; } } DogController.java Model and View
  • 35.
    35 Path Variables @Controller @RequestMapping("/cat") public classCatController { @GetMapping("/edit/{catId}") @ResponseBody public String editCat(@PathVariable long catId){ return String.valueOf(catId); } } CatController.java Path Variable
  • 36.
  • 37.
  • 38.
  • 39.
    39 Application Properties #Data SourceProperties spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/cat_store? useSSL=false&createDatabaseIfNotExist=true spring.datasource.username=root spring.datasource.password=1234 #JPA Properties spring.jpa.properties.hibernate.dialect=org.hibernate.dialect .MySQL5InnoDBDialect spring.jpa.properties.hibernate.format_sql=TRUE spring.jpa.hibernate.ddl-auto=update application.properties
  • 40.
    40  Entity isa lightweight persistence domain object Entities @Entity @Table(name = "cats") public class Cat { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; //GETTERS AND SETTERS } Cat.java
  • 41.
    41  Persistence layerthat works with entities Repositories @Repository public interface CatRepository extends CrudRepository<Cat, Long> { } CatRepository.java
  • 42.
    42  Business Layer.All the business logic is here. Services @Service public class CatServiceImpl implements CatService { @Autowired private CatRepository catRepository; @Override public void buyCat(CatModel catModel) { //TODO Implement the method } } CatService.java
  • 43.
  • 44.
    44  Spring Boot- Opinionated view of building production-ready Spring applications  Spring MVC - MVC framework that has three main components:  Controller - controls the application flow  View - presentation layer  Model - data component with the main logic  Spring Data - Responsible for database related operations Summary
  • 45.
    ? ? ? ? ? ? ? ? ? Questions? Web Development Basics– Course Overview https://softuni.bg/courses/
  • 46.
    License  This course(slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license 46
  • 47.
    Free Trainings @Software University  Software University Foundation – softuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg  Software University @ Facebook  facebook.com/SoftwareUniversity  Software University @ YouTube  youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bg

Editor's Notes

  • #28 Blue Color – Provided by Spring Purple Color – To Be Implemented by the Developer Green Color – Provided by Spring. Sometimes implemented by the developer