标签: MVC

Spring MVC Interceptor 实现性能监控

利用Spring MVC 的 Interceptor 实现个简易的性能监控,计算一下每个url的执行时间。在 preHandle 方法中记录当前的时间戳到线程局部变量里,然后在afterCompletion方法中,用当前的时间戳剪掉线程局部变量里的时间戳得出个运行时间,并记录到log里。这只是个简易的性能监控,如果想长期正式的监控性能,请选择其它成熟的产品。Interceptor1234567891011121314151617181920212223242526272829303132333435363738import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.NamedThreadLocal;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class PerformanceInterceptor implements HandlerInterceptor { private final static Logger LOGGER = LoggerFactory.getLogger(PerformanceInterceptor.class); private NamedThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("performance"); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { startTimeThreadLocal.set(System.currentTimeMillis()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { return; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { long currentTime = System.currentTimeMillis(); long executeTime = currentTime - startTimeThreadLocal.get(); LOGGER.info("uri:{} 执行了 {} 毫秒", request.getRequestURI(), executeTime); }}

SpringMVC 引用Jar中的 jQuery、BootStrap 和 Font Awesome

无意中在Maven公服中发现bootstrap经被打包到jar中了。http://mvnrepository.com/artifact/org.webjars/bootstrap123456789101112131415<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>1.11.1</version></dependency><dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.5</version></dependency><dependency> <groupId>org.webjars</groupId> <artifactId>font-awesome</artifactId> <version>4.6.3</version></dependency>