简体   繁体   English

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

[英]How to profile the aspect creation process of Spring AOP?

Suppose I want to create an aspect which checks whether my repository method returns null or not: 假设我想创建一个方面来检查我的存储库方法是否返回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()
    {
    }
}

This takes any method which has the @NonNullReturning annotation and automatically throws an exception if it wants to return with null . 这将采用任何具有@NonNullReturning批注的方法,并且如果要返回null ,则会自动引发异常。

What I want to achieve is to profile how much memory these aspects soak up and how much time Spring spends creating them? 我想要实现的是分析这些方面吸收了多少内存以及Spring花了多少时间创建它们?

The aspect works right now in a local scope but I should be able to tell how much resources will it take up when I start up Jetty (or Tomcat) with this aspect enabled application-wide. 该方面现在可以在本地范围内工作,但是当我在应用程序范围内启用该方面时,我应该能够知道在启动Jetty(或Tomcat)时将占用多少资源。

Edit: I'm worried about the application startup time. 编辑:我担心应用程序的启动时间。

I have used following to see execution time of spring framework using aspectj.Just you need to download aspectjrt.jar and Configure and convert to aspectj project in ecclipse and you need source file of spring. 我已经使用下面的代码查看了使用Aspectj的spring框架的执行时间,只需要下载Aspectjrt.jar并在ecclipse中配置并转换为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;
    }


}

Answer to edit one :You need to see log how sequence of flow goes for bean creation ,that can be achieved by adding log4j.logger.org.springframework=ALL from there when server starts you can get little information on how the bean instantiation sequence goes on in container.Likewise you can customize bean creation. 编辑一个答案 :您需要查看日志以了解如何创建Bean流程,这可以通过在服务器启动时从此处添加log4j.logger.org.springframework = ALL来实现,您几乎无法获得有关Bean实例化序列的信息。继续在容器中。同样,您可以自定义Bean创建。 In log4j.properties put 在log4j.properties中放

log4j.rootLogger=INFO,stdout, file log4j.rootLogger = INFO,stdout,文件

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

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

log4j.category.org.springframework=ALL log4j.category.org.springframework =全部

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

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

log4j.appender.file.Encoding=UTF-8 log4j.appender.file.Encoding = UTF-8

log4j.appender.file.MaxFileSize=40000000KB log4j.appender.file.MaxFileSize = 40000000KB

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

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

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

which package level you want to log org.springframework.aop is upper package and config is package under org.springframework.aop log4j.category.org.springframework.aop.config=DEBUG log4j.category.org.springframework.transaction=DEBUG 您要登录哪个软件包级别org.springframework.aop是上层软件包,而config是org.springframework.aop下的软件包log4j.category.org.springframework.aop.config = DEBUG log4j.category.org.springframework.transaction = DEBUG

see log in test.log Or in eclipse console folderUnderRootWhereApplicationServerResides means if tomcat is installed in D then create folderUnderRootWhereApplicationServerResides folder in D drive 请参阅登录test.log或在Eclipse控制台中找到folderUnderRootWhereApplicationServerResides,表示如果Tomcat已安装在D中,则在D驱动器中创建folderUnderRootWhereApplicationServerResides文件夹

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM