Slide 1

Slide 1 text

Spring-­‐Petclinic  

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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   | | && && +

Slide 4

Slide 4 text

Topics   •  Core  Spring   •  The  Web  layer  

Slide 5

Slide 5 text

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)

Slide 6

Slide 6 text

Bean  profiles   business-­‐config.xml     3  profiles   default  (JPA)   jdbc   Spring  Data  JPA   Inside web.xml spring.profiles.active jdbc 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)

Slide 7

Slide 7 text

Caching   •  The  list  of  Veterinarians  is  cached  using  ehcache   @Cacheable(value = "vets") public Collection findVets() throws DataAccessException { … } ClinicServiceImpl tools-config.xml ehcache.xml

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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  

Slide 10

Slide 10 text

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/

Slide 11

Slide 11 text

Topics   •  Core  Spring   •  The  Web  layer   –  Spring  MVC   –  Third-­‐party  web  libraries  

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Topics   •  Core  Spring   •  The  Web  layer   –  Spring  MVC   –  Third-­‐party  web  libraries   •  Dandelion  for  datatables     •  Webjars  

Slide 14

Slide 14 text

•  Based on project Dandelion –  http://dandelion.github.com/ Datatables in Spring MVC 14 JSP file

Slide 15

Slide 15 text

•  Click, sort, scroll, next/previous… •  Bootstrap theme •  PDF export… Dandelion is based on jQuery Datatables and Bootstrap

Slide 16

Slide 16 text

Topics   •  Core  Spring   •  The  Web  layer   –  Spring  MVC   –  Third-­‐party  web  libraries   •  Dandelion     •  WebJars  

Slide 17

Slide 17 text

•  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

Slide 18

Slide 18 text

Webjars org.webjars jquery-ui 1.9.1 pom.xml

Slide 19

Slide 19 text

Using Webjars 19 •  Inside pom.xml Spring configuration •  Inside JSP org.webjars jquery-ui 1.9.1 。js file is inside a jar file!

Slide 20

Slide 20 text

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