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

spring-petclinic-sample-application

 spring-petclinic-sample-application

关于Spring Petclinic示例应用的简单介绍,附有图解和示例代码。

Avatar for Xiao Liou

Xiao Liou

April 05, 2016

Other Decks in Programming

Transcript

  1. Views Controller Service Repository JSP + ⾃定义tag Thymeleaf || Bootstrap3

    (CSS) Bower Dandelion && + && Spring @MVC 注解 Bean 校验 @Cacheable @Transactional Spring Data JPA default (JPA) jdbc 3 profiles 3
  2. 数据访问 VisitRepository JpaVisitRepositoryImpl JdbcVisitRepositoryImpl SpringDataVisitRepository findByPetId: (短短)5行 代码 findByPetId: 5行代码

    findByPetId: 0行代码 (基于命名约定的Interface 方式声明) 可以在web.xml中设置相应的profile (包括jdbc、jpa及spring-data-jpa) 来选择使用某个具体的实现 5
  3. Bean profiles Spring Data JPA default (JPA) jdbc business-config.xml 3种profiles

    如果选择默认的profile (JPA),则无需做任何配置 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({…}) @WebAppConfiguration @ActiveProfiles("spring-data-jpa") public class OwnerControllerTests { … } <context-param> <param-name> spring.profiles.active </param-name> <param-value> spring-data-jpa </param-value> </context-param> Junit测试代码 web.xml代码 6
  4. Caching(缓存) @Cacheable(value = "vets") public Collection<Vet> findVets() throws DataAccessException {

    … } <cache name="vets" timeToLiveSeconds="60” maxElementsInMemory="100” … /> ClinicServiceImpl • 宠物医生列表采用ehcache进行缓存 ehcache.xml <!-- 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:cache/ehcache.xml"/> </bean> tools-config.xml 7
  5. 异常处理 SimpleMappingExceptionResolver ClinicService PetController PetRepository 一旦抛出RuntimeException异常, 则事务会被回滚 (异常还是会传播给PetController) 这里不处理异常, 异常被往后传播

    可能抛出RuntimeException异常 (此处通常为DataAccessException) 在mvc-core-config.xml中声明 基于配置其作用如下: • 记录异常的栈跟踪信息 • 传递给WEB-INF/jsp/exception.jsp • 异常被显示在exception.jsp页面 8
  6. 面向切面编程(2/2) • CallMonitoringAspect @Repository public class JpaVisitRepositoryImpl @Repository public class

    JpaVetRepositoryImpl … @Aspect public class CallMonitoringAspect { @Around("within(@org.springframework.stereotype.Repository *)") public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable { … } } 如想进一步了解在Spring框架中AOP(面向切面编程)是如何工作的,可以阅读以下博客文章: https://spring.io/blog/2012/05/23/transactions-caching-and-aop-understanding-proxy-usage-in-spring 织入监控(monitoring) 织入监控 (monitoring) 织入监控 (monitoring) 10
  7. 议题 • Core Spring • Web层 −Spring MVC −第3方的Web库 •

    来自Dandelion库的Datatables (数据表格) • Bower包管理器 13
  8. Spring MVC中的表格(Datatables) • 基于Dandelion项目 ҆https://github.com/dandelion <datatables:table id="owners" data="${selections}" row="owner" cssClass="table

    table-striped" pageable="true" info="false" export="pdf"> <datatables:column titleKey="name" cssStyle="width: 150px;" display="html"> <spring:url value="/owners/{ownerId}.html" var="ownerUrl"> <spring:param name="ownerId" value="${owner.id}"/> </spring:url> <a href="${fn:escapeXml(ownerUrl)}"> <c:choose> <c:when test='${fn:startsWith(pageContext.response.locale, "zh")}'> <c:out value="${owner.lastName}, ${owner.firstName}"/> </c:when> <c:otherwise> <c:out value="${owner.firstName} ${owner.lastName}"/> </c:otherwise> </c:choose> </a> </datatables:column> <datatables:column titleKey="address" property="address" cssStyle="width: 200px;"/> … <datatables:export type="pdf" cssClass="btn" cssStyle="height: 25px;"/> </datatables:table> JSP文件 14
  9. 议题 • Core Spring • Web层 −Spring MVC −第3方的Web库 •

    来自Dandelion库的Datatables (数据表格) • Bower包管理器 16
  10. 使用Bower安装的资源 18 <mvc:resources mapping="/vendors/**" location="/vendors/"/> Spring配置 <%-- Bootstrap CSS --%>

    <spring:url value="/vendors/bootstrap/dist/css/bootstrap.min.css" var="bootstrapCss"/> <link href="${bootstrapCss}" rel="stylesheet"> <spring:url value="/vendors/bootstrap/dist/css/bootstrap-theme.min.css" var="bootstrapThemeCss"/> <link href="${bootstrapThemeCss}" rel="stylesheet"> JSP文件 (htmlHeader.jsp、footer.jsp) <%-- Placed at the end of the document so the pages load faster --%> <spring:url value="/vendors/jquery/jquery.min.js" var="jQuery"/> <script src="${jQuery}"></script> <%-- jquery-ui.js file is really big so we only load what we need instead of loading everything --%> <spring:url value="/vendors/jquery-ui/ui/jquery.ui.core.js" var="jQueryUiCore"/> <script src="${jQueryUiCore}"></script> <spring:url value="/vendors/jquery-ui/ui/jquery.ui.datepicker.js" var="jQueryUiDatePicker"/> <script src="${jQueryUiDatePicker}"></script> <%-- Bootstrap --%> <spring:url value="/vendors/bootstrap/dist/js/bootstrap.min.js" var="bootstrapJs"/> <script src="${bootstrapJs}"></script>
  11. 参考 • Julien Dubois写的关于如何“改进Spring-Petclinic 应用性能(improve performance of the Spring- Petclinic

    application)”的五篇系列博客文章 −http://blog.ippon.fr/2013/03/11 • Dandelion: 在Spring-Petclinic中使用的可点击/可 排序的数据表格 −http://dandelion.github.io/blog/2013/04/24/Integrating -Dandelion-DataTables-in-the-Spring-Petclinic-app/ 19