Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring 1 day program


Published on

It is a one day program for beginners in spring

Published in: Software
  • Be the first to comment

Spring 1 day program

  1. 1. Simple, Portable, Fast and Flexible By Mohit Kanwar
  2. 2. Foreword  This a basic introduction to Spring Framework and niche technologies  To understand technical aspects it is recommended to have prior knowledge of  Java  Maven  SDLC  Non-Functional Requirements  Git  Make this discussion yours : peer to peer learning
  3. 3. Overview Introduction to Spring Working with Spring Spring Boot Spring Security Spring Test
  4. 4. Introduction to Spring
  5. 5. Overview What is Spring? 1 Whats in it for me? 2 Basic Concepts •Dependency Injection •IOC Container •Aspect Oriented Programming 3 Spring Architecture •Spring Beans 4 Other Spring Projects 5
  6. 6. What is Spring Spring is • An Application Development Framework • Inversion of Control Container • Open Source • Alternate to Enterprise Java Beans Spring Allows us • To create an HTTP endpoint, without dealing with servlet API • To Focus on functionality of a method, without having to deal with transactions API
  7. 7. What’s in it for me? Infrastructure Focused Development Aspects Open Source Existing Technologies Automation Testing Fast Modular Huge Community Independent of Container Easy to understand Transactional Reliable Configurable Extensible Secure Customizable Cost Effective Lightweight
  8. 8. Dependency Injection  An Implementation of  Factory Design Pattern  What to instantiate?  Builder Design Pattern  How to instantiate?  Service Locator Design Pattern  Where to find it?
  9. 9. Dependency Injection  It is at the heart of Spring  It helps keeping our classes as independent as possible  Increases reuse by applying low coupling  Easy testing  Readable  Passes the dependency (a service) to a dependent object (a client) rather than allowing a client to build or find the service
  10. 10. IOC Container  The container gets its instructions on what objects to instantiate, configure and assemble by reading configuration metadata provided.  The configuration metadata can be represented either by  XML  Java annotations  Java code
  11. 11. Spring Beans singleton A single instance per Spring IoC container (default). prototype New Instance whenever requested request Once every HTTP request session Once per HTTP Session. global- session Once per global HTTP Session  Spring beans are  The objects that form the backbone of an application  Managed by Spring IOC Container Scope
  12. 12. Dependency Injection XML Config Foo f = new Foo("Cleopatra"); Bar b = new Bar("Arthur",26); b.setFoo(f);
  13. 13. Aspect Oriented Programming Accounts Business Logic Persistence Security Logging Users Business Logic Persistence Security Logging Bill Payment Business Logic Persistence Security Logging Location Finder Business Logic Persistence Security Logging Non AOP
  14. 14. Aspect Oriented Programming Accounts Business Logic Users Business Logic Bill Payment Business Logic Location Finder Business Logic AOP PersistenceSecurityLogging Caching
  15. 15. Aspect Oriented Programming  @Before – Run before the method execution  @After – Run after the method returned a result  @AfterReturning – Run after the method returned a result, intercept the returned result as well.  @Around – Run around the method execution, combine all three advices above.  @AfterThrowing – Run after the method throws an exception Pointcut (AspectJ Pointcut Expression Language) Join Point Advice
  16. 16. Spring Framework Architecture
  17. 17. Typical Full Fledged Web Application Custom Domain Logic Aspects Web Views APIs Persistence Models Notifications Remote Automation Testing
  18. 18. Open Source
  19. 19. Various Spring Projects Spring-Boot Spring-Batch Spring- Security Spring- Framework Spring-Cloud Spring- Integration
  20. 20. Summary What is Spring? 1 Whats in it for me? 2 Basic Concepts •Dependency Injection •IOC Container •Aspect Oriented Programming 3 Spring Architecture •Spring Beans 4 Other Spring Projects 5
  21. 21. Spring Boot
  22. 22. Spring Boot  Spring Boot is a brand new framework from the team at Pivotal, designed to simplify the bootstrapping and development of a new Spring application. The framework takes an opinionated approach to configuration, freeing developers from the need to define boilerplate configuration.  Features  Create stand-alone Spring applications  Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)  Provide opinionated 'starter' POMs to simplify your Maven configuration  Automatically configure Spring whenever possible  Provide production-ready features such as metrics, health checks and externalized configuration  Absolutely no code generation and no requirement for XML configuration
  23. 23. Working with Spring
  24. 24. Overview Setting up workspace 1 Hello World in Spring 2 Spring Web- MVC 3 Spring Transactions 4 Aspects 5
  25. 25. My First Spring Based Application  Creating Environment  Choosing dependencies  Setting it all up!
  26. 26. Spring Boot it Up  Spring Initializr 
  27. 27. Demo
  28. 28. Hello World! My first Rest API using Spring
  29. 29. Spring Boot Maven Plugin Uber Jar • It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service. Runnable Class • It searches for the public static void main() method to flag as a runnable class. Dependency Resolver • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
  30. 30. Automation Test
  31. 31. @Autowired  Helps finding a desired implementation spring bean  Inject its instance  Get rid of new keyword  Implementations can be injected at runtime instead of compile time.
  32. 32. Hello World Controller • Makes the class a Spring Aware MVC Controller @Controller • Map a HTTP request method to Bean method @RequestMapping • Sets the returned value from method as the HTTP response body @ResponseBody
  33. 33. Spring Boot Starter @SpringBootApplication @Configuration @EnableAutoConfiguration @ComponentScan
  34. 34. Build and Run the Jar  mvn clean install  java -jar target/demo-0.0.1-SNAPSHOT.jar Test http://localhost:8080/
  35. 35. Hello Web!  Understanding MVC Architecture  Understanding Templates  First Spring based web-app
  36. 36. MVC Architecture
  37. 37. Spring MVC Architecture
  38. 38. The DispatcherServlet The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.
  39. 39. Spring boot Devtools  Enable hot swapping  Switches template engines to disable caching  Enables LiveReload to refresh browser automatically  Other reasonable defaults based on development instead of production
  40. 40. Demo GreetingController /greeting Model Template (greeting.html) Response
  41. 41. Transactions!
  42. 42. Transaction  A transaction is a set of instructions which are treated as a single unit of work  They should complete entirely or nothing Atomicity • Single unit of operation • It should either be complete or not at all • No middle state Consistency • Consistency of referential integrity Isolation • Each transaction must be isolated from others Durability • Changes are stored permanently • Must not be erased due to system failure
  43. 43. Transaction Commit and Rollback State 1 Step 1 Step 2 Step 3 State 2 Commit State 1 Rollback State after Transaction Ends Error
  44. 44. Aspects!
  45. 45. Important Definitions  Aspects  Cross cutting concerns of an application e.g. Logging, Transaction Management  Join Point  A point where an aspect is expected  Advice  Action taken at particular Join Point  Pointcut  Determine weather an advice is to be executed at a Join Point
  46. 46. Spring Security
  47. 47. Spring Security  Spring Security is a framework that focuses on providing both authentication and authorization to Java Applications  Features  Comprehensive and extensible support for both Authentication and Authorization  Protection against attacks like session fixation, clickjacking, CSRF etc  Servlet API integration  Optional integration with Spring Web MVC
  48. 48. Authentication and Authorization
  49. 49. Spring Test
  50. 50. Spring Test  Spring Test Framework supports  Unit testing with mock objects  IOC container to create dependencies for integration testing  Transaction management for integration testing  Third party frameworks like  Junit  TestNG  Mockito
  51. 51. References  getting-started-with-spring.html         boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html      spring-mvc-applications-security/
  52. 52. Further reading S.No Title Resource 1 War Packaging boot/docs/current/reference/htmlsingle/#buil d-tool-plugins-maven-packaging 2 Getting Started ing-framework-reference/html/overview- getting-started-with-spring.html 3 Testing with non- default options 4 Spring Security security-and-angular-js/ 6 Annotations autowired_annotation.htm 7 DevTools boot/docs/current/reference/html/using- boot-devtools.html
  53. 53. About Me  Mohit Kanwar  Email :   Social     Blog 