带有 weblogic 的 AspectJ [英] AspectJ with weblogic

查看:32
本文介绍了带有 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(Load Time Weaving)

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. 您要编织的目标类:所有你想看的东西都应该在类路径中.对于典型的应用程序,它们将在您的应用程序 WEB-INF/lib 或 WEB-INF/classes 中,所以让它们在那里.这里没有变化.

  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:Weaver 使用它来发现 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 编译器)编译了它,那么它也可以放在引导类路径中(但与 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中的标签中,而不是你想要Aspect on的类/包列表

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

  • 摆弄引导类路径(就像我一样),
  • 任何类路径中的aspectjrt.jar

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

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