问题
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
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>
Here is my aspect file:
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)
}
This aspect is compiled (using normal javac compiler) into foo.jar
I am running Weblogic by adding the following:
java -javaagent:<path_to aspectweaver.jar> -Xbootclasspath/p:<path to {foo.jar, aspectweaver.jar}>
I believe this is most likely a classpath/classloader issue due to which AspectJ is not able to create "aspectOf()" methods in DefaultAspect class
Please help.
回答1:
Found the answer. Note that this is only for LTW (Load Time Weaving)
There are primarily 4 key components from a weaving perspective:
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: 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).
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).
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
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 If using setDomainEnv.sh, you will need to do an EXPORT JAVA_OPTIONS also.
So, for LTW there is really no need of
- fiddling with the bootclasspath (as I was),
- aspectjrt.jar in any of the classpaths
回答2:
Have you tried to also add aspectjrt.jar to the classpath? Actually it should be a subset of aspectweaver.jar and not necessary, but maybe you want to try it anyway.
Update: Hm, maybe the problem is that you put your aspect on the boot classpath instead of the normal classpath and even prepend it, i.e. you make it be found first, maybe even before the Java agent. Maybe you want to change that.
Because I am not an application server user and not a Weblogic expert in particular, you might want to consult http://rajiv-kuriakose.blogspot.de/2011/03/aspectj-example-with-weblogic-server.html for a sample configuration.
来源:https://stackoverflow.com/questions/23590662/aspectj-with-weblogic