对于web系统,我觉得用spring可以解决,写个拦截器,记录每个方法的执行时间,写入日志就行了,但对于非web系统呢,假如有个c/s系统,如何着手?

解决方案 »

  1.   

    自己用java.lang.reflect.Proxy实现一个拦截器
      

  2.   

    搞笑!Spring 又不是只能用在 B/S 应用中
      

  3.   

    用java_home/bin/jvisualvm.exe看哪个方法或类占用cpu时间最长
      

  4.   

    spring当然能用在c/s里,但它能使用拦截器吗?c/s有filter吗?除了getBean没看到任何可以用的地方。
      

  5.   

    jprofile
    这个怎么用?有啥效果
      

  6.   

    拦截器类文件 WebSessionInterceptor.java public class WebSessionInterceptor extends HandlerInterceptorAdapter 

        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 
        { 
            //获取URL请求的相对路径信息URI的前缀部分,形如:/servlet/test.do的test 
            String lookupPath = new UrlPathHelper().getLookupPathForRequest(request); 
    //        logger.debug(lookupPath); 
            WebSession webSession = null; 
            try 
            { 
                webSession = handler.getClass().getDeclaredMethod(lookupPath, HttpServletRequest.class, HttpServletResponse.class) 
                        .getAnnotation(WebSession.class); 
            } 
            catch (Exception e) 
            { 
                // 
            } 
            if (webSession == null) webSession = handler.getClass().getAnnotation(WebSession.class); 
            //do something... 
        } 
    } spring配置文件 servlet-do.xml
        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
            <property name="interceptors">
                <list>
                    <ref bean="webSessionInterceptor"/>
                </list>
            </property>
        </bean>    <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>    <bean id="webSessionInterceptor" class="frame.web.mvc.WebSessionInterceptor"/>
    控制器类文件 MainController.java
    @Controller
    @WebSession
    public class MainController
    {
        private final Logger logger = LoggerFactory.getLogger(getClass());   @WebSession 
       @RequestMapping
        public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception
        {
            return new ModelAndView("main/index.jsp");
        }   @WebSession 
       @RequestMapping
        public ModelAndView make(HttpServletRequest request) throws Exception
        {
            return new ModelAndView("main/index.jsp");
        }
    }
    请求/main.index.do时,系统能够执行MainController中的方法index,拦截器类也可以根据index方法的参数项来获取Method和Method上声明的Annotation项@WebSession。
      

  7.   

    使用netbeans,sun官方网站可以下载,和eclipse差不多的IDE。里面有performance profiler,可以定位到method级别,可以看到每个方法运行所需时间,所占内存,cpu等一些信息。
      

  8.   

    你需要一个profile工具,例如jprofiler.