+ All Categories
Home > Documents > Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request...

Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request...

Date post: 05-Apr-2018
Category:
Upload: lytruc
View: 215 times
Download: 1 times
Share this document with a friend
25
nortal.com Veebirakenduste loomine - Spring Kristjan Talvar, Nortal AS
Transcript
Page 1: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

nortal.com

Veebirakenduste loomine - Spring

Kristjan Talvar, Nortal AS

Page 2: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Framework• Spring• Demo using Spring Boot

2Topics

Page 3: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Java and Spring dev for over two years• Estonian Unemployment fund• TETRIS, REDIS, EMPIS• AngularJS, REST, Intellij, Gradle, Spring, Aranea, Tomcat• Mercural, Jira, Confluence, Bamboo, Fisheye

3About me

Page 4: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• A web framework is a software framework designed to simplify your web development life.

• Frameworks exist to save you from having to re-invent the wheel and help alleviate some of the overhead when building a new site.

• Sessions and cookies• Rest• Database• Template engines

4What is a Web Framework?

Page 5: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Lead by pivotal• Open source application• Plugins and support for most web technologies• Loads of utility API-s

5Spring Framework

Page 6: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Free• Easy to use• No XML anymore• Extremely popular• Java, Groovy and Kotlin support• Good for cloud and microservices

6Should I bother?

Page 7: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Your code depends on abstractions, Spring handles actual implementations

• Can switch implementations easily

7Dependency injection

Page 8: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

MessageService.java

123456

package com.journaldev.spring.di.services;

public interface MessageService {

boolean sendMessage(String msg, String rec);}

8Example

EmailService.java

12345678

package com.journaldev.spring.di.services;

public class EmailService implements MessageService {

public boolean sendMessage(String msg, String rec) {System.out.println("Email Sent to "+rec+ " with

Message="+msg);return true;

}

Source: http://www.journaldev.com/2410/spring-dependency-injection-example-with-annotations-and-xml-configuration

Page 9: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

1. private MessageService service;2. @Autowired3. public void setService(MessageService svc){4. this.service=svc;5. }6. public boolean processMessage(String msg, String rec){7. //some magic like validation, logging etc8. return this.service.sendMessage(msg, rec);9. }

9Usage

Page 10: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• How much code do I have to write in Java to do a simple DB query?

10Question

Page 11: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Interface + one line of code• public interface UserRepository extends CrudRepository

<User, Long> { Long countByLastname(String lastname);

• }• http://docs.spring.io/spring-

data/jpa/docs/current/reference/html/#repositories.core-concepts

11Awnser

Page 12: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Don’t have to write XML anymore• Annotation and Bean based configuration• Easier setup and ease of use• New innovative features

12Spring trends

Page 13: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• @RestController• @Autowired• @RequestMapping("/add")• @RepositoryRestResource• @Scheduled(fixedRate = 5000)

13Annotations

Page 14: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

14MVC

Page 15: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Is a Map interface• Complete abstraction• Integrate directly with JSP, Velocity, Freemarker, Thymeleaf• Directly generate XML, JSON, Atom

• Extra: http://docs.spring.io/autorepo/docs/spring/4.1.x/spring-framework-reference/html/mvc.html

15Model

Page 16: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Handles requests• Initialize and request mapping• Declare request parameters or path variables• @RequestParam/@PathVariable• Allows creation of RESTful sites and apps• Creates models• Delegates rendering requests

16Controllers

Page 17: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

17

Page 18: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Very important feature of a framework is non-intrusiveness

• String MVC normally lets you do stuff according to the MVC pattern

• But id doesn't’t prevent you from violating MVC if you really want to

18Non-intrusive

Page 19: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• What disappears and what is replaced• What role does Spring MVC play• Where will development shift?• Front End vs Back end• Isomorphic javascript and project Nashhorn

19Front end

Page 20: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Representational state transfer• Stateless design• Software architecture style consisting of guidelines and

best practices for scalable web services• RESTful system• Communicate over HTTP with GET,POST,PUT,DELETE

20REST

Page 21: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Controller moves• Rest Services• Stateless

21What Changes

http://blog.jhades.org/developing-a-modern-java-8-web-app-with-spring-mvc-and-angularjs/

Page 22: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Conventions and backwards compability• Flexible designs, reusable• Different schools of thoughts• More technologies• Singe-page application• Data transfer objects

22Spring + JS framework or templating engine

Page 23: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• @SpringBootApplication• Demo: https://github.com/kristjant/Spring-demo• Stand-alone Spring apps• Embedded web server, no war• Automatically configure Spring whenever possible• Absolutely no code generation and no requirement for

XML configuration• Link and documents:• http://projects.spring.io/spring-boot/

23Spring boot

Page 24: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Spring has excellent documentation:• http://docs.spring.io/spring/docs/current/spring-framework-

reference/htmlsingle/• Tutorials:• https://spring.io/guides• Sample apps:• https://github.com/spring-projects/spring-

boot/tree/master/spring-boot-samples• MVC: http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-

framework-reference/html/mvc.html

24Useful links

Page 25: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable

• Questions and answers• Contact: [email protected]

25Thank you!


Recommended