Spring Boot Introduction Spring MVC Spring Data Sprin
Spring Boot Introduction Spring MVC, Spring Data Sprin Intro g Boot duct ion Soft. Uni Team Technical Trainers Software University http: //softuni. bg
Table of Contents 1. Spring Boot Components 2. Spring MVC 3. Spring Data 2
Have a Question? sli. do #Java. Web 3
What is Spring Boot? 4
Spring Boot § Opinionated view of building production-ready Spring applications Tomcat pom. xml Spring Boot Auto configuration 5
Spring Boot 6
Creating Spring Boot Project § Just go to https: //start. spring. io/ 7
Spring Dev Tools § Additional set of tools that can make the application development faster and more enjoyable pom. xml <dependency> <group. Id>org. springframework. boot</group. Id> <artifact. Id>spring-boot-devtools</artifact. Id> <scope>runtime</scope> </dependency> 8
Spring Dev Tools Setup 9
Spring Resources HTML, CSS, JS Thymeleaf App propertues 10
Spring Boot Main Components § Four main components: § Spring Boot Starters - combine a group of common or related dependencies into single dependency § Spring Boot Auto. Configurator - reduce the Spring Configuration § Spring Boot CLI - run and test Spring Boot applications from command prompt § Spring Boot Actuator – provides End. Points and Metrics 11
Spring Boot Starters (1) pom. xml <dependency> <group. Id>org. springframework. boot</group. Id> <artifact. Id>spring-boot-starter-web</artifact. Id> <version>1. 4. 1. RELEASE</version> </dependency> 12
Spring Boot Starters (2) spring-boot-starter-web spring-boot-starter-tomcat spring-web spring-boot-starter spring-web-mvc spring-boot tomcat-embed-logging-juli spring-boot-autoconfigure tomcat-embed-core spring-boot-starter-logging 13
Spring Boot Auto. Configurator pom. xml @Target({Element. Type. TYPE}) @Retention(Retention. Policy. RUNTIME) @Documented @Inherited @Spring. Boot. Configuration @Enable. Auto. Configuration @Component. Scan( exclude. Filters = {@Filter( type = Filter. Type. CUSTOM, classes = {Type. Exclude. Filter. class} )} ) public @interface Spring. Boot. Application 14
Spring Boot CLI § Command Line Interface - Spring Boot software to run and test Spring Boot applications 15
Spring Boot Actuator § Expose different types of information about the running application pom. xml <dependency> <group. Id>org. springframework. boot</group. Id> <artifact. Id>spring-boot-starter-actuator</artifact. Id> </dependency> 16
Inversion of Control § Spring provides Inversion of Control and Dependency Injection User. Service. Impl. java //Tradiotional Way public class User. Service. Impl implements User. Service { private User. Repository user. Repository = new User. Repository(); } User. Service. Impl. java //Dependency Injection @Service public class User. Service. Impl implements User. Service { @Autowired private User. Repository user. Repository; } 17
Spring Io. C Meta Data: 1. XML Config 2. Java Config 3. Annotation Config Automatic Beans: 1. @Component 2. @Service 3. @Repository Explicit Beans 1. @Bean Io. C Fully Configured System 18
Beans § Object that is instantiated, assembled, and otherwise managed by a Spring Io. C container Dog. java public class Dog implements Animal { private String name; public Dog() {} //GETTERS AND SETTERS } 19
Bean Declaration Dog. java @Spring. Boot. Application public class Main. Application { … @Bean public Animal get. Dog(){ return new Dog(); } Bean Declaration } 20
Get Bean from Application Context Main. Application. java Main. Application @Spring. Boot. Application public class Main. Application { public static void main(String[] args) { Application. Context context = Spring. Application. run(Main. Application. class, args); Animal dog = context. get. Bean(Dog. class); System. out. println("DOG: " + dog. get. Class(). get. Simple. Name()); } } 21
Bean Lifecycle Instantiation Initialization Set Properties Set Name Pre Initialization Set Application Context When Container is Shutdown Post Initialization Bean is ready Bean is destroyed 22
Bean Lifecycle Demo (1) Main. Application. java Main. Application @Spring. Boot. Application public class Main. Application { public static void main(String[] args) { Application. Context context = Spring. Application. run(Main. Application. class, args); ((Abstract. Application. Context)context). close(); } @Bean(destroy. Method = "destroy", init. Method = "init") public Animal get. Dog(){ return new Dog(); } } 23
Bean Lifecycle Demo (2) Main. Application. java Main. Application 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. . "); } } 24
Bean Scope § The default one is Singleton. It is easy to change to Prototype Mostly used as State-less Singleton Request A Request B Request C Dog Mostly used as State-full Prototype Request A Dog 1 Request B Dog 2 Request C Dog 3 25
Bean Scope Demo 26
What is Spring MVC? 27
What is Spring MVC? § Model-view-controller (MVC) framework is designed around a Dispatcher. Servlet that dispatches requests to handlers Find Controller Request Handler Mapping Controller Service Dispatcher Execute Handler Action Adapter Servlet View Name Repository Resolve View Response View Resolver Result Model DB Model Business Logic 28
MVC – Control Flow Web Client Request Controller Update Model Notify Model Update View Response (html, json, xml) User Action View 29
Controllers Controller Dog. Controller. java Dog. Controller @Controller public class Dog. Controller { Print Text } Request Mapping @Get. Mapping("/dog") @Response. Body Action public String get. Dog. Home. Page(){ return "I am a dog page"; } Text 30
Actions – Get Requests Cat. Controller. java Cat. Controller @Controller public class Cat. Controller { Request Mapping @Get. Mapping("/cat") public String get. Home. Cat. Page(){ Action return "cat-page. html"; } } View 31
Actions – Post Requests (1) Cat. Controller. java Cat. Controller @Controller Starting route @Request. Mapping("/cat") public class Cat. Controller { @Get. Mapping("") public String get. Home. Cat. Page(){ return "new-cat. html"; } } 32
Actions – Post Requests (1) Cat. Controller. java Cat. Controller @Request. Mapping("/cat") public class Cat. Controller { Request param @Post. Mapping("") public String add. Cat(@Request. Param String cat. Name, @Request. Param int cat. Age){ System. out. println(String. format("Cat Name: %s, Cat Age: %d", cat. Name, cat. Age)); Redirect return "redirect: /cat"; } } 33
Models and Views Dog. Controller. java Dog. Controller @Controller public class Dog. Controller { Model and View @Get. Mapping("/dog") public Model. And. View get. Dog. Home. Page(Model. And. View model. And. View){ model. And. View. set. View. Name("dog-page. html"); return model. And. View; } } 34
Path Variables Cat. Controller. java Cat. Controller @Request. Mapping("/cat") public class Cat. Controller { Path Variable @Get. Mapping("/edit/{cat. Id}") @Response. Body public String edit. Cat(@Path. Variable long cat. Id){ return String. value. Of(cat. Id); } } 35
Spring MVC Demo 36
Spring Data 37
Overall Architecture Repository Service Controller View Database Entities Models/ DTO Back-end 38
Application Properties application. properties #Data Source Properties spring. datasource. driver. Class. Name=com. mysql. jdbc. Driver spring. datasource. url=jdbc: mysql: //localhost: 3306/cat_store? use. SSL= false&create. Database. If. Not. Exist=true spring. datasource. username=root spring. datasource. password=1234 #JPA Properties spring. jpa. properties. hibernate. dialect=org. hibernate. dialect. My. SQL 5 Inno. DBDialect spring. jpa. properties. hibernate. format_sql=TRUE spring. jpa. hibernate. ddl-auto=update 39
Entities § Entity is a lightweight persistence domain object Cat. java Cat @Entity @Table(name = "cats") public class Cat { @Id @Generated. Value(strategy = Generation. Type. IDENTITY) private long id; private String name; //GETTERS AND SETTERS } 40
Repositories § Persistence layer that works with entities Cat. Repository. java Cat. Repository @Repository public interface Cat. Repository extends Crud. Repository< Cat, Long> { } 41
Services § Business Layer. All the business logic is here. Cat. Service. java Cat. Service @Service public class Cat. Service. Impl implements Cat. Service { @Autowired private Cat. Repository cat. Repository; @Override public void buy. Cat(Cat. Model cat. Model) { //TODO Implement the method } } 42
Spring Data Demo 43
Summary § 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 44
Web Development Basics – Course Overview ? s n stio e u Q ? ? ? https: //softuni. bg/courses/
License § This course (slides, examples, demos, videos, homework, etc. ) is licensed under the "Creative Commons Attribution. Non. Commercial-Share. Alike 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/Software. University § Software University @ You. Tube § youtube. com/Software. University § Software University Forums – forum. softuni. bg
- Slides: 47