0

假设我想创建一个方面来检查我的存储库方法是否返回 null:

@Aspect
public class NonNullReturningAspect
{

    @Around("anyPublicRepositoryMethod()  && args(pk,..  )")
    public Object checkNullResults(ProceedingJoinPoint joinPoint, Long pk) throws Throwable
    {    
        Object result = joinPoint.proceed();    
        if (result == null && hasNonNullReturningAnnotation(joinPoint))
        {
            Class<?> returnType = calculateReturnType(joinPoint);
            throw new ObjectNotFoundException(returnType, pk);
        }
        return result;
    }

    // ...

    @Pointcut(value = "execution(public * somepackage..repository..*.*(..))")
    private void anyPublicRepositoryMethod()
    {
    }

    @Pointcut("@annotation(NonNullReturning)")
    private void nonNullReturning()
    {
    }
}

这需要任何具有@NonNullReturning注释的方法,如果它想要返回,则会自动抛出异常null

我想要实现的是分析这些方面吸收了多少内存以及 Spring 花费了多少时间来创建它们?

该方面现在在本地范围内工作,但我应该能够知道当我启动 Jetty(或 Tomcat)并在应用程序范围内启用此方面时它将占用多少资源。

编辑:我担心应用程序的启动时间。

4

1 回答 1

0

我用下面的方法查看了使用aspectj的spring框架的执行时间。只需要下载aspectjrt.jar并在eclipse中配置并转换为aspectj项目,并且需要spring的源文件。

package org.springframework;

import java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AspectJAnnotation {

    protected long starttime;
    protected long endtime;
    protected  ExecutionVaribale methodlevleinformation=new ExecutionVaribale();
    protected   List<ExecutionVaribale> liststore = new ArrayList<ExecutionVaribale>(); 







    @Before("execution(* *.*())") // this will call on before every method call on spring framework Or you can change it depending on which spring package method you can to monitor
    public void beforeAnymethodcall(final JoinPoint thisJoinPoint) {


        starttime = System.currentTimeMillis();
        methodlevleinformation.setMethodname(thisJoinPoint.getStaticPart().getSignature().getName());
    }

    @After("execution(* *.*())") // this will call on after every method call on spring framework Or you can change it depending on which spring package method you can to monitor
    public void afterAnymethodcall(final JoinPoint thisJoinPoint) {
        endtime = System.currentTimeMillis();

        methodlevleinformation.setDifferenetime((starttime - endtime)/ 1000); // time in secs
        liststore.add(methodlevleinformation);
        // at last you can iterate list and see the results .Hope this will provide a fine grain information on time taken by spring framework 




    }

     class ExecutionVaribale{


        /**
         * @return the differenetime
         */
        public long getDifferenetime() {
            return differenetime;
        }
        /**
         * @param differenetime the differenetime to set
         */
        public void setDifferenetime(long differenetime) {
            this.differenetime = differenetime;
        }
        /**
         * @return the methodname
         */
        public String getMethodname() {
            return methodname;
        }
        /**
         * @param methodname the methodname to set
         */
        public void setMethodname(String methodname) {
            this.methodname = methodname;
        }

        protected long differenetime;
        protected String  methodname;
    }


}

编辑一个答案:您需要查看记录 bean 创建的流程顺序,这可以通过在服务器启动时从那里添加 log4j.logger.org.springframework=ALL 来实现,您几乎无法获得有关 bean 实例化顺序的信息在容器中继续。同样,您可以自定义 bean 创建。在 log4j.properties 中放

log4j.rootLogger=INFO、标准输出、文件

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.category.org.springframework=ALL

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=/folderUnderRootWhereApplicationServerResides/test.log

log4j.appender.file.Encoding=UTF-8

log4j.appender.file.MaxFileSize=40000000KB

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

您要记录的包级别 org.springframework.aop 是上层包,config 是 org.springframework.aop log4j.category.org.springframework.aop.config=DEBUG log4j.category.org.springframework.transaction=DEBUG 下的包

请参阅登录 test.log 或 eclipse 控制台中的 folderUnderRootWhereApplicationServerResides 表示如果 tomcat 安装在 D 中,则在 D 驱动器中创建 folderUnderRootWhereApplicationServerResides 文件夹

http://www.mkyong.com/logging/log4j-log4j-properties-examples/

于 2013-05-06T14:52:59.793 回答