Can';t为AspectJ添加javaagent

Can';t为AspectJ添加javaagent,java,spring,jboss,aspectj,spring-aop,Java,Spring,Jboss,Aspectj,Spring Aop,我有一个旧的Java6应用程序,我正在尝试添加一些自动和全局日志来跟踪每个方法中的每个调用。 它运行在JBoss4.2.3上 我举一个例子: 我有我的评测课程: package com.al6.borneIntranet.business.log; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.

我有一个旧的Java6应用程序,我正在尝试添加一些自动和全局日志来跟踪每个方法中的每个调用。 它运行在JBoss4.2.3上

我举一个例子:

我有我的评测课程:

package com.al6.borneIntranet.business.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
import org.springframework.core.annotation.Order;

@Aspect
public class ProfilingAspect {

    @Around("methodsToBeProfiled()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch(getClass().getSimpleName());
        try {
            sw.start(pjp.getSignature().getName());
            return pjp.proceed();
        } finally {
            sw.stop();
            System.out.println(sw.prettyPrint());
        }
    }

    @Pointcut("execution(public * com.al6.borneIntranet..*.*(..))")
    public void methodsToBeProfiled(){}
}
我创建了一个META-INF/aop.xml文件:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="com.al6.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.al6.borneIntranet.business.log.ProfilingAspect"/>
    </aspects>

</aspectj>
我从maven存储库下载spring-instrument-4.0.6.RELEASE.jar文件,然后在运行启动脚本中添加javaagent部分:

/opt/jdk/bin/java -Dprogram.name=run.sh -javaagent:/opt/jboss/lib/org.springframework.instrument.jar -server -Xms2048m -Xmx2048m -XX:MaxPermSize=256M ...
但是错误仍然出现,好像我什么都没做

有没有办法在启动时不轻易添加此选项? 为什么即使我添加了它,它也没有被使用

编辑:

我尝试添加adspectj启动代理,但也不起作用,出现相同的错误,应用程序似乎未检测到该选项:

2021-04-29 18:41:46,960 ERROR [STDERR] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [org.jboss.mx.loading.UnifiedClassLoader3] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar 
/bin/sh /opt/jboss/bin/run.sh -c commandes -b 0.0.0.0 -Djboss.partition.name=jboss-commande-preprod1-partition 
admin    22263 47.6 76.7 4911056 4023536 pts/0 Sl   16:03   2:14 /opt/jdk/bin/java -Dprogram.name=run.sh -javaagent:/opt/jboss/lib/aspectjweaver-1.8.9.jar -javaagent:/opt/jboss/lib/org.springframework.instrument.jar -server -Xms3500m -Xmx
3500m -XX:MaxPermSize=512M -Dfile.encoding=UTF-8 -Djava.awt.headless=true -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dc
om.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -XX:+UseG1GC -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n -Djava.net.prefer
IPv4Stack=true -Djava.endorsed.dirs=/opt/jboss/lib/endorsed -classpath /opt/jboss/bin/run.jar:/opt/jdk/lib/tools.jar org.jboss.Main -c commandes -b 0.0.0.0 -Djboss.partition.name=jboss-commande-preprod1-partition
编辑2:

尝试几件事:

  • 使用Jboss timeweaver:

    创建名为“loadTimeWeaver”的bean时出错:bean的实例化失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.instrument.classloading.jboss.JBossLoadTimeWeaver]:构造函数引发异常;嵌套异常为java.lang.IllegalStateException:无法初始化JBoss LoadTimeWeaver,因为JBoss 6 API类不可用

当然,我使用的是JBoss4.3.2

  • 在aop.xml中添加“-verbose-showWeaveInfo-debug”:
检测到该文件,因为我在启动时有以下行:

2021-05-03 15:11:42,267 ERROR [STDERR] [UnifiedClassLoader3@341a736] info AspectJ Weaver Version 1.8.9 built on Monday Mar 14, 2016 at 21:18:16 GMT
2021-05-03 15:11:43,253 ERROR [STDERR] [UnifiedClassLoader3@341a736] info register classloader org.jboss.mx.loading.UnifiedClassLoader3@341a736
2021-05-03 15:11:43,253 ERROR [STDERR] [UnifiedClassLoader3@341a736] info using configuration file:/opt/jboss-4.2.3.GA/server/weldom_commandes/tmp/deploy/tmp6458410370486804467BorneIntranetServeurMetier.ear-contents/BorneIntranetServeurMetier.jar!/META-INF/aop.xml
2021-05-03 15:11:43,253 ERROR [STDERR] [UnifiedClassLoader3@341a736] info using configuration file:/opt/jboss-4.2.3.GA/server/weldom_commandes/tmp/deploy/tmp6458410370486804467BorneIntranetServeurMetier.ear-contents/spring-aspects-4.0.6.RELEASE.jar!/META-INF/aop.xml
2021-05-03 15:11:43,259 ERROR [STDERR] [UnifiedClassLoader3@341a736] info register aspect com.al6.borneIntranet.business.log.ProfilingAspect
2021-05-03 15:11:43,474 ERROR [STDERR] [UnifiedClassLoader3@341a736] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
2021-05-03 15:11:43,520 ERROR [STDERR] [UnifiedClassLoader3@341a736] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
2021-05-03 15:11:43,536 ERROR [STDERR] [UnifiedClassLoader3@341a736] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
2021-05-03 15:11:43,544 ERROR [STDERR] [UnifiedClassLoader3@341a736] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
2021-05-03 15:11:43,559 ERROR [STDERR] [UnifiedClassLoader3@341a736] debug not weaving 'com.al6.borneIntranet.business.ServiceArchiveEntryPointBean'
...
  • 当我提出:

    <context:load-time-weaver
              weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    
    但我已经加上:

    -javaagent:/opt/jboss/lib/org.springframework.instrument.jar
    
    ls -lrt /opt/jboss/lib : 
    -rwxrwxrwx 1 admin users 7244 2021-04-30 16:03 /opt/jboss/lib/org.springframework.instrument.jar*
    

    我不是Spring用户,但在这里回答AspectJ LTW问题时,我经常注意到Spring文档中提到只需要Spring Instrument Java代理,而实际情况中有这样配置Spring的方法,但这很棘手。到目前为止,最简单的方法是将AspectJ Weaver代理添加到命令行,也就是说,使用类似(当然是在单行上)的东西

    #AspectJ LTW
    -javaagent:/path/to/aspectjweaver.jar
    #弹簧乐器
    -javaagent:/path/to/org.springframework.instrument.jar
    
    也许AspectJ Weaver已经足够了,这取决于您的用例


    更新在OP更新他的问题后:

    • 您是否在应用服务器日志中看到AspectJ weaver正在加载的迹象?使用configuration…/aop.xml扫描字符串,如
      AspectJ Weaver Version
      register classloader
      。如果您正确地将
      aspectjweaver.jar
      META-INF/aop.xml
      添加到项目中,您应该会看到类似的内容,即使由于某种原因该方面没有被触发
    • 确保Java进程找到了
      META-INF/aop.xml
      。AspectJ通过
      Classloader.getResource()
      找到它。因此,无论您将它放在什么位置或JAR中,都要确保该位置位于类路径上。通过实验,您可以向类路径添加一个本地目录,并查看JVM是否在那里找到XML文件。然后,如果您发现了这种方法,您可以开始改进您的方法
    • 顺便说一句,我看到的
      只会直接匹配包
      com.al6
      中的类。如果要包含子包,则需要使用
      语法,即
    • 此外,您还链接到了最新的Spring手册中关于本机AspectJ+LTW配置的指南。您更应该使用,因为您使用的是旧版本。我已经在下面的评论中给你发送了一个链接
    请先尝试通过添加
    -javaagent:/path/to/aspectjweaver.jar
    来显示我在第一个项目符号中提到的字符串,然后再尝试其他操作。只有这样,您才可能希望优化或微调:


    对于JBoss,有一个特殊的
    JBossLoadTimeWeaver
    。它应该是自动检测的,但您也可以手动指定,请参阅。

    无法找到加载时编织不起作用的原因

    所以,有人在我的工作只是问我为什么要做加载时编织,为什么不只是做“编译时编织”

    我只是在pom.xml中添加了以下内容:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
            <complianceLevel>1.6</complianceLevel>
            <source>1.6</source>
            <target>1.6</target>
            <showWeaveInfo>true</showWeaveInfo>
            <verbose>true</verbose>
            <Xlint>ignore</Xlint>
            <encoding>UTF-8 </encoding>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <!-- use this goal to weave all your main classes -->
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    
    org.codehaus.mojo
    aspectj maven插件
    1.7
    1.6
    1.6
    1.6
    真的
    真的
    忽视
    UTF-8
    编译
    
    我删除了javaagent的东西

    建造,运行,塔达,一切正常

    我不明白为什么几乎没有人谈论这个方法,它不需要在启动参数中添加任何东西,任何复杂的东西,而且工作起来容易得多

    这有很大的不便吗?
    事实上,我想知道它是否更好,因为它在编译时编织类,所以它将使用更少的系统资源?

    Thx,但奇怪的是,它没有改变任何东西。我修改的模块生成和EAR,它部署在:/opt/jboss-4.2.3.GA/server/commandes/deploy/BorneIntranetServeurMetier.earI使用完整的启动命令更新原始帖子您的启动命令启动shell脚本。也许您想检查该脚本是否按预期转发参数。如果没有,只需修改脚本,以确保参数到达Java命令行。所有选项如Xmx、Xms都正常工作,脚本run.sh是jboss本机脚本,它采用定义良好的Java_选项。是否有其他方法不需要在启动时添加此选项?thxThx,我用一些细节编辑了我的答案。我用更多信息更新了我的答案。看看它是否有助于你取得进步。这是一种变通办法,而不是解决你问题的办法。如果lLTW不工作,问题通常出现在键盘前面。有了所要求的MCVE,我本可以帮助你的。请理解,将任何Java代理(不仅仅是AspectJ)添加到容器和应用程序服务器(使用多个classl)中
    2021-05-03 15:15:03,475 ERROR [STDERR] java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
    2021-05-03 15:15:03,476 ERROR [STDERR]  at org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver.addTransformer(InstrumentationLoadTimeWeaver.java:88)
    
    -javaagent:/opt/jboss/lib/org.springframework.instrument.jar
    
    ls -lrt /opt/jboss/lib : 
    -rwxrwxrwx 1 admin users 7244 2021-04-30 16:03 /opt/jboss/lib/org.springframework.instrument.jar*
    
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
            <complianceLevel>1.6</complianceLevel>
            <source>1.6</source>
            <target>1.6</target>
            <showWeaveInfo>true</showWeaveInfo>
            <verbose>true</verbose>
            <Xlint>ignore</Xlint>
            <encoding>UTF-8 </encoding>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <!-- use this goal to weave all your main classes -->
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>