Spring Petclinic sample application

Spring Petclinic sample application

Diagrams and code samples that give a brief overview of the Spring Petclinic sample application.

94e7fdd6b2dc215dd59777be08c744c9?s=128

Michael Isvy

March 20, 2013
Tweet

Transcript

  1. Spring-­‐Petclinic  

  2. Spring  Petclinic   •  h/ps://github.com/SpringSource/spring-­‐petclinic   •  h/p://spring-­‐petclinic.cloudfoundry.com/  

  3. 3  profiles   jdbc   default  (JPA)   Spring  Data

     JPA   Repository Service @Cacheable   @TransacGonal   Controller Bean  ValidaGon   Spring  @MVC  annotaGons   Views Bootstrap  (CSS)   JSP  with     custom  tags   Thymeleaf   Dandelion   webjars   | | && && +
  4. Topics   •  Core  Spring   •  The  Web  layer

     
  5. Data  Access   VisitRepository   JdbcVisitRepository   JpaVisitRepo   SpringDataJpa

      VisitRepo   findByPetId: 16 lines of code findByPetId: 6 (short) lines of code findByPetId: 0 lines (interface declaration is enough based on naming conventions) In order to select which implementation should be used, select the Appropriate bean profile inside web.xml (jdbc, jpa or spring-data-jpa)
  6. Bean  profiles   business-­‐config.xml     3  profiles   default

     (JPA)   jdbc   Spring  Data  JPA   Inside web.xml <context-param> <param-name> spring.profiles.active </param-name> <param-value> jdbc </param-value> </context-param> Inside JUnit tests @ContextConfiguration(locations = …) @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("jdbc") public class JdbcOwnerRepositoryTests …{} No configuration needed in case you wish to use th default profile (JPA)
  7. Caching   •  The  list  of  Veterinarians  is  cached  using

     ehcache   @Cacheable(value = "vets") public Collection<Vet> findVets() throws DataAccessException { … } ClinicServiceImpl <!-- enables scanning for @Cacheable annotation --> <cache:annotation-driven/> <bean id="cacheManager" class="org...EhCacheCacheManager"> <property name="cacheManager" ref="ehcache"/> </bean> <bean id="ehcache" class="org...EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> tools-config.xml <cache name="vets" timeToLiveSeconds="60" maxElementsInMemory="100" … /> ehcache.xml
  8. ExcepGon  Handling   PetRepository   ClinicService   PetController   May

    throw a RuntimeException (typically DataAccessException) Transaction is rolled back in case of a RuntimeException (exception is still propagated to PetController) Exception is not handled there It is propagated. SimpleMapping   ExcepGonResolver   Declared in mvc-core-config.xml Based on the configuration used in petclinic: •  Logs the exception stacktrace •  Forwards to WEB-INF/jsp/exception.jsp •  Exception logged as a comment inside exception.jsp
  9. Aspect  Oriented  Programming  (1/2)   •  How  to  add  behavior

     in  all  methods  of  all  Repository   classes?   JpaOwnerRepository   JpaPetRepository   JpaVetRepository   JpaVisitRepository   ClinicService   LOG  ALL   METHOD   CALLS  
  10. Aspect  Oriented  Programming  (2/2)   •  CallMonitoringAspect   …  

    @Repository public class JpaVetRepositoryImpl   @Repository public class JpaVisitRepositoryImpl   @Aspect public class CallMonitoringAspect { @Around("within(@org.springframework.stereotype.Repository *)") public Object invoke(…) { … } } Adds monitoring Adds monitoring Adds monitoring To understand further how AOP works in Spring: http://blog.springsource.org/2012/05/23/understanding-proxy-usage-in-spring/
  11. Topics   •  Core  Spring   •  The  Web  layer

      –  Spring  MVC   –  Third-­‐party  web  libraries  
  12. View  Resolvers  in  spring-­‐petclinic   12   ContentNegoGaGngVR   Does

    not resolve any view on its own Delegates to other view resolvers BeanNameVR   Atom and XML InternalResourceVR   Default viewClass: JstlView (used for JSP files) vets.html owners.html vets.xml pets/9/visits.atom mvc-view-config.xml
  13. Topics   •  Core  Spring   •  The  Web  layer

      –  Spring  MVC   –  Third-­‐party  web  libraries   •  Dandelion  for  datatables     •  Webjars  
  14. •  Based on project Dandelion –  http://dandelion.github.com/ Datatables in Spring

    MVC 14 <datatables:table data="${userList}" id="dataTable”> <datatables:column title="First Name" property="firstName" sortable="true" /> <datatables:column title="Last Name" property="lastName" sortable="true" /> </datatables:table> JSP file
  15. •  Click, sort, scroll, next/previous… •  Bootstrap theme •  PDF

    export… Dandelion is based on jQuery Datatables and Bootstrap
  16. Topics   •  Core  Spring   •  The  Web  layer

      –  Spring  MVC   –  Third-­‐party  web  libraries   •  Dandelion     •  WebJars  
  17. •  Allow CSS and JS libraries to be imported as

    Maven libraries –  Used in Petclinic for jQuery, jQuery-ui, datatables, Bootstrap –  http://www.webjars.org/ Webjars
  18. Webjars <dependency> <groupId>org.webjars</groupId> <artifactId>jquery-ui</artifactId> <version>1.9.1</version> </dependency> pom.xml

  19. Using Webjars 19 •  Inside pom.xml Spring configuration •  Inside

    JSP <dependency> <groupId>org.webjars</groupId> <artifactId>jquery-ui</artifactId> <version>1.9.1</version> </dependency> <link rel=“stylesheet" href=“/webjars/jquery-ui/1.9.1/js/jquery- ui-1.9.1.custom.js"> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/ resources/webjars/"/> 。js file is inside a jar file!
  20. References   •  Series  of  5  blog  entries  from  Julien

     Dubois  on  how  to   “improve  performance  of  the  Spring-­‐Petclinic  applicaGon”   –  h/p://blog.ippon.fr/2013/03/11/   •  Dandelion:  clickable/sortable  datatatables  inside  Spring-­‐ Petclinic   –  h/p://dandelion.github.io/blog/2013/04/24/IntegraGng-­‐Dandelion-­‐ DataTables-­‐in-­‐the-­‐Spring-­‐Petclinic-­‐app   20