veebirakenduste loomine - spring · model 15 • handles requests • initialize and request...
TRANSCRIPT
![Page 1: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/2.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/3.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/4.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/5.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/6.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/7.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/10.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/11.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/12.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/13.jpg)
• @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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/14.jpg)
14MVC
![Page 15: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/15.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/16.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/17.jpg)
17
![Page 18: Veebirakenduste loomine - Spring · Model 15 • Handles requests • Initialize and request mapping • Declare request parameters or path variables • @RequestParam/@PathVariable](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/18.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/19.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/20.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/21.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/22.jpg)
• 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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/23.jpg)
• @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](https://reader034.vdocuments.mx/reader034/viewer/2022051800/5ac5f9c17f8b9a5c558daa29/html5/thumbnails/24.jpg)
• 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