如何描述Spring AOP的方面创建过程?

时间:2013-05-06 14:08:32

标签: java spring aspectj spring-aop

假设我想创建一个方面来检查我的存储库方法是否返回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)时,我应该能够知道它将占用多少资源。

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

1 个答案:

答案 0 :(得分:0)

我使用了以下来看看使用aspectj的spring框架的执行时间。你需要在ecclipse中下载aspectjrt.jar并配置并转换为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,stdout,file

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是上层包,配置是org.springframework.aop下的包 log4j.category.org.springframework.aop.config = DEBUG  log4j.category.org.springframework.transaction = DEBUG

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

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