Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Michael Isvy's presentation about Spring Petclinic

Michael Isvy
September 13, 2013
630

Michael Isvy's presentation about Spring Petclinic

Michael Isvy

September 13, 2013
Tweet

Transcript

  1. 1
    Pivotal Confidential–Internal Use Only 1
    © Copyright 2013 Pivotal. All rights reserved.
    Spring PetClinic
    August 2013

    View full-size slide

  2. 2
    Pivotal Confidential–Internal Use Only
    Michael  Isvy
    Ÿ  Training Manager (APJ region)
    –  Joined SpringSource in 2008
    –  Already taught Spring in more than 20 countries
    ▪  Core-Spring, Spring MVC, Spring with JPA/Hibernate
    Ÿ  In charge of the Spring Petclinic sample app
    Ÿ  Blog: http://blog.springsource.org/author/misvy/
    twitter: @michaelisvy

    View full-size slide

  3. 3
    Pivotal Confidential–Internal Use Only
    From Spring to Pivotal

    View full-size slide

  4. 4
    Pivotal Confidential–Internal Use Only
    Topics  
    Ÿ  Core  Spring  
    Ÿ  The  Web  layer  

    View full-size slide

  5. 5
    Pivotal Confidential–Internal Use Only
    Spring  Petclinic  
    Ÿ  Spring  sample  applica:on  
    Ÿ  Major  update  in  March  2013  
    Ÿ  hBps://github.com/SpringSource/spring-­‐petclinic  

    View full-size slide

  6. 6
    Pivotal Confidential–Internal Use Only
    General  architecture  
    Repository
    Service
    Controller
    Database

    View full-size slide

  7. 7
    Pivotal Confidential–Internal Use Only
    Topics  
    Ÿ  Core  Spring  
    –  Database  access  
    Ÿ  The  Web  layer  

    View full-size slide

  8. 8
    Pivotal Confidential–Internal Use Only
    10  years  ago:  Plain  JDBC  
    public List findByLastName(String lastName) {
    List personList = new ArrayList();
    Connection conn = null;
    String sql = “select first_name, age from PERSON where last_name=?“;
    try {
    DataSource dataSource = DataSourceUtils.getDataSource();
    conn = dataSource.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setString(1, lastName);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
    String firstName = rs.getString(”first_name“);
    int age = rs.getInt(“age”);
    personList.add(new Person(firstName, lastName, age));
    }
    } catch (SQLException e) { /* ??? */ }
    finally {
    try {
    conn.close();
    } catch (SQLException e) { /* ??? */ }
    }
    return personList;
    }

    View full-size slide

  9. 9
    Pivotal Confidential–Internal Use Only
    Data  Access  in  Spring  Petclinic  
    Ÿ  3  possibili:es  
    VisitRepository
    JdbcVisitRepository JpaVisitRepo SpringDataJpa
    VisitRepo
    findByPetId: 16 lines of code
    Based on Spring’s
    JdbcTemplate
    findByPetId: 6 (short)
    lines of code
    findByPetId: 0 lines (interface
    declaration is enough based on
    naming conventions)

    View full-size slide

  10. 10
    Pivotal Confidential–Internal Use Only
    @Query  
    import org.springframework.data.repository.Repository;
    import org.springframework.data.jpa.repository.Query;
    public interface UserRepository extends Repository {
    save(S entity); // Definition as per CRUDRepository
    User findById(long i); // Query determined from method name
    User findByNameIgnoreCase(String name); // Case insensitive search
    @Query("select u from User u where u.emailAddress = ?1")
    User findByEmail(String email); // ?1 replaced by method param
    }

    View full-size slide

  11. 11
    Pivotal Confidential–Internal Use Only
    Spring  Data  at  run:me  
    •  Before  startup  
    AOer  startup  
    Interface
    UserRepository
    Interface
    UserRepository
    $Proxy1
    implements
    You can conveniently use Spring to inject a dependency of type
    UserRepository. Implementation will be generated at startup time.

    View full-size slide

  12. 12
    Pivotal Confidential–Internal Use Only
    Spring  Data  projects  
    •  Syntax  similar  for  all  sub-­‐projects  
     
     
     
     
     
     
     
    Spring Data
    JPA vFabric
    GemFire
    MongoDB
    Apache
    Hadoop REST
    JDBC
    extensions
    And many more ...
    Core project
    Sub-projects

    View full-size slide

  13. 13
    Pivotal Confidential–Internal Use Only
    Bean  profiles  
    dao-config.xml
    3 profiles
    jdbc
    JPA
    Spring Data JPA
    Inside web.xml


    spring.profiles.active


    jdbc


    Inside JUnit tests
    @ContextConfiguration(locations = …)
    @RunWith(SpringJUnit4ClassRunner.class)
    @ActiveProfiles("jdbc")
    public class JdbcOwnerRepositoryTests
    …{}

    View full-size slide

  14. 14
    Pivotal Confidential–Internal Use Only
    Transac:ons  
    Ÿ  In  the  code:  
    Ÿ  In  the  configura:on:  

    View full-size slide

  15. 15
    Pivotal Confidential–Internal Use Only
    Caching  
    Ÿ  The  list  of  Veterinarians  is  cached  using  ehcache  
    @Cacheable(value = "vets")
    public Collection findVets() throws
    DataAccessException { … }
    ClinicServiceImpl







    tools-config.xml
    timeToLiveSeconds="60"
    maxElementsInMemory="100" … />
    ehcache.xml

    View full-size slide

  16. 16
    Pivotal Confidential–Internal Use Only
    Topics  
    Ÿ  Core  Spring  
    Ÿ  The  Web  layer  

    View full-size slide

  17. 17
    Pivotal Confidential–Internal Use Only
    Why Spring MVC?
    Ÿ  InfoQ top 20 Web frameworks for the JVM
    –  Spring MVC number 1
    http://www.infoq.com/research/jvm-web-frameworks

    View full-size slide

  18. 18
    Pivotal Confidential–Internal Use Only
    How to use Spring MVC?
    Ÿ  Which way is more appropriate?
    18
    public class UserController
    extends SimpleFormController {
    public ModelAndView
    onSubmit(Object command) { //... }
    }
    @Controller
    public class UserController {
    @RequestMapping(value="/users/",
    method=RequestMethod.POST)
    public ModelAndView createUser(User user) 

    { //... }
    }
    Deprecated!!

    View full-size slide

  19. 19
    Pivotal Confidential–Internal Use Only
    View Layer
    Ÿ  Form tag library
    19


    Enter your first name:


    ...

    View full-size slide

  20. 20
    Pivotal Confidential–Internal Use Only
    Bootstrap
    Ÿ  Let’s talk about CSS…

    View full-size slide

  21. 21
    Pivotal Confidential–Internal Use Only
    Why Bootstrap?
    Ÿ  So anybody can make a good web design
    Let’s talk about Bootstrap!

    View full-size slide

  22. 22
    Pivotal Confidential–Internal Use Only
    What is Bootstrap?
    Ÿ  Originally called “Twitter Bootstrap”
    Ÿ  Available from 2011
    Ÿ  Typography, forms, buttons, charts, navigation and other
    interface components
    Ÿ  Integrates well with jQuery

    View full-size slide

  23. 23
    Pivotal Confidential–Internal Use Only
    What is Bootstrap?
    Ÿ  Most popular project on github!
    https://github.com/popular/starred

    View full-size slide

  24. 24
    Pivotal Confidential–Internal Use Only
    Bootstrap themes
    Ÿ  Hundreds of themes available
    –  So your website does not look like all other websites!
    –  Some are free and some are commercial
    Ÿ  Example: www.bootswatch.com/

    View full-size slide

  25. 25
    Pivotal Confidential–Internal Use Only
    Avoiding JSP soup
    JSP  file
    HTML
    Javascript
    CSS
    Taglibs

    View full-size slide

  26. 26
    Pivotal Confidential–Internal Use Only
    Form fields: Without custom tags

    ${lastNameLabel}







    CSS div
    Label
    Form input
    Error message (if any)
    JSP

    View full-size slide

  27. 27
    Pivotal Confidential–Internal Use Only
    Using custom tags
    Ÿ  First create a tag (or tagx) file
    <%@ taglib prefix="form" uri="http://www.spring…org/tags/form" %>
    <%@ attribute name="name" required="true" rtexprvalue="true" %>
    <%@ attribute name="label" required="true" rtexprvalue="true" %>

    ${label}







    inputField.tag
    Custom tags are part of Java EE

    View full-size slide

  28. 28
    Pivotal Confidential–Internal Use Only
    Using custom tags
    Ÿ  Custom tag call Folder which contains
    custom tags





    JSP file
    1 line of code
    instead of 9!!
    No more JSP soup!

    View full-size slide

  29. 29
    Pivotal Confidential–Internal Use Only
    jQuery  
    29  

    View full-size slide

  30. 30
    Pivotal Confidential–Internal Use Only
    jQuery inside Spring Petclinic
    Ÿ  Javascript framework
    Ÿ  Very simple core with thousands of plugins available
    –  Datatable
    –  jQuery UI (datepicker, form interaction…)

    View full-size slide

  31. 31
    Pivotal Confidential–Internal Use Only
    Why jQuery?

    View full-size slide

  32. 32
    Pivotal Confidential–Internal Use Only
    Datatables in Spring MVC
    Ÿ  Based on project Dandelion
    –  dandelion.github.com
    –  Twitter: @dandelion_proj

    property="firstName" sortable="true" />
    property="lastName" sortable="true" />

    JSP file

    View full-size slide

  33. 33
    Pivotal Confidential–Internal Use Only
    Dandelion is based on
    jQuery Datatables and Bootstrap
    Ÿ  Click, sort, scroll, next/previous…
    Ÿ  Bootstrap theme
    Ÿ  PDF export…

    View full-size slide

  34. 34
    Pivotal Confidential–Internal Use Only
    Conclusion
    Ÿ  Data  Access  
    –  Consider  using  Spring  Data  
    Ÿ  Web  
    –  Spring  MVC:  most  popular  Java  Framework  
    –  Use  custom  tags!  
    –  Consider  using  Dandelion  for  Datatables  
    ▪  hBp://dandelion.github.com/  
    34

    View full-size slide

  35. 35
    Pivotal Confidential–Internal Use Only
    Why Spring and Hadoop tonight?
    Ÿ  Pivotal has several projects in the Apache Hadoop
    ecosystem
    –  To be discussed by Steve after the break!
    Ÿ  Spring Data Hadoop
    –  Uses the same annotations as for Spring Data JPA
    –  Hadoop without the boilerplate code

    View full-size slide

  36. 36
    Pivotal Confidential–Internal Use Only
    Learning more about Spring and Hadoop
    Ÿ  Get Certified!
    Ÿ  Upcoming classes in Singapore
    –  Information: email Michael Isvy ( [email protected] )
    Course Date
    Core-Spring September 17-20
    Core-Spring November 26-29
    Hadoop October 22-25
    Hadoop January 14-16 2013

    View full-size slide