使用Weblogic的AspectJ [英] AspectJ with weblogic

查看:193
本文介绍了使用Weblogic的AspectJ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LTW在Weblogic上运行AspectJ.我的切入点适用于公共构造函数和方法,建议适用于Before,AfterReturning和AfterThrowing.访问简单的"Hello World" jsp时出现以下错误:

I am trying to run AspectJ on Weblogic with LTW. My pointcut is for public constructor and methods, and advices are for Before, AfterReturning and AfterThrowing. I am getting following error when I access a simple "Hello World" jsp:

javax.servlet.ServletException: Servlet class: 'jsp_servlet.__index' doesn't have a default constructor
        at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.run(StubSecurityHelper.java:315)
        at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.run(StubSecurityHelper.java:288)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
        Truncated. see log file for complete stacktrace

Root cause of ServletException.
java.lang.NoSuchMethodError: foo.aspect.DefaultAspect.aspectOf()Lfoo/aspect/DefaultAspect;
       at jsp_servlet.__index._jspService(__index.java:76)
       at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
       at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
       at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
       at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
       Truncated. see log file for complete stacktrace

这是我的aop.xml文件:

Here is my aop.xml file:

<aspectj>
    <aspects>
        <aspect name="foo.aspect.DefaultAspect" />
    </aspects>
    <weaver options="-verbose">
        <include within="foo.aspect.*" />
        <include within="jsp_servlet..*"/>
    </weaver>
</aspectj>         

这是我的方面文件:

package foo.aspect;

@Aspect
public class DefaultAspect  {

@Pointcut("execution(public *.new(..)) && !within(foo.aspect.*)")
public void pointCutNew(JoinPoint thisJoinPoint) {
}

@After("pointCutNew(thisJoinPoint)")
public void adviceForNew(JoinPoint thisJoinPoint) {
}
//Simiar pattern for other advices and pointcuts (for methods)
}

此方面(使用常规javac编译器)编译为foo.jar

This aspect is compiled (using normal javac compiler) into foo.jar

我通过添加以下内容来运行Weblogic:

I am running Weblogic by adding the following:

java -javaagent:<path_to aspectweaver.jar> -Xbootclasspath/p:<path to {foo.jar, aspectweaver.jar}>

我认为这很可能是类路径/类加载器的问题,这是由于AspectJ无法在DefaultAspect类中创建"aspectOf()"方法

I believe this is most likely a classpath/classloader issue due to which AspectJ is not able to create "aspectOf()" methods in DefaultAspect class

请帮助.

推荐答案

找到了答案.请注意,这仅适用于LTW(加载时间编织)

Found the answer. Note that this is only for LTW (Load Time Weaving)

从编织的角度来看,主要有4个关键组成部分:

There are primarily 4 key components from a weaving perspective:

  1. 您要编织的目标类: 您要关注Aspect的所有对象都应该在类路径中.对于典型的应用程序,它们将位于您的应用程序WEB-INF/lib或WEB-INF/classs中,因此让它们在那里.这里没有变化.

  1. The target classes that you want to weave: All those that you want to Aspect on should be in the classpath. For a typical application, they will be in your applications WEB-INF/lib or WEB-INF/classes, so let them be there. No changes here.

AOP.xml: 编织者使用它来发现Aspect和Weaver配置.这也应该在类路径中可用.您可以将其放在/lib文件夹中的JAR中,以便所有应用程序(EAR和WAR)都可以使用它的配置.

AOP.xml: This is used by the weaver to discover the Aspect and Weaver configuration. This should also be available in the classpath. You can put this in a JAR in /lib folder, so that its configuration is available for all applications (EARs and WARs).

Aspect类: 如果对Aspect类使用注释,则也需要对其进行编织". AspectJ weaver向此类添加了一些特殊方法(如AspectOf).因此,它必须在类路径中可用.这可以是与(2)相同的JAR的一部分.如果您已经使用ajc(aspectJ编译器)对其进行了编译,则也可以将其放入bootclasspath中(但与lib文件夹相比并没有任何真正的优势).

The Aspect class(es): If using annotations for Aspect class, then "it also needs to be weaved". AspectJ weaver adds some special methods (like aspectOf) to this class. Hence it must be available in the classpath. This can be part of the same JAR as for (2). If you've already compiled this using ajc (the aspectJ compiler), then it can be put in the bootclasspath also (but giving no real advantage over the lib folder).

注意:由于需要编织此类,因此它必须存在于AOP.xml的标记中,而不是您要在其上进行外观编程的类/程序包的列表

Note: Since this class needs to be woven, it must be present in the tag in the AOP.xml, other than the list of classes/packages you want to Aspect on

  1. Weaver本身(位于Aspectjweaver.jar中):应该可以通过Java代理使用,因此将以下行添加到/bin/setDomainEnv.cmd

  1. The weaver itself (which is in aspectjweaver.jar): This should be available via java agent, so add the following line to the /bin/setDomainEnv.cmd

SET JAVA_OPTIONS =%JAVA_OPTIONS%-javaagent:%ASPECT_HOME%\ lib \ aspectjweaver.jar 如果使用setDomainEnv.sh,则还需要执行EXPORT JAVA_OPTIONS.

SET JAVA_OPTIONS=%JAVA_OPTIONS% -javaagent:%ASPECT_HOME%\lib\aspectjweaver.jar If using setDomainEnv.sh, you will need to do an EXPORT JAVA_OPTIONS also.

因此,对于LTW,实际上不需要

So, for LTW there is really no need of

  • 摆弄bootclasspath(就像我以前一样),
  • 任意类路径中的
  • aspectjrt.jar

这篇关于使用Weblogic的AspectJ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆