An attempt at AspectJ support

282 views
Skip to first unread message

Adam Gent

unread,
May 17, 2011, 7:31:16 AM5/17/11
to Ehcache Spring Annotations
I am making rough attempt to get @Cacheable to work with AspectJ
instead of proxies that I will post at
https://github.com/agentgt/ehcache-spring-annotations

Below is the aspectj aspect I am using for @Cacheable:


package com.googlecode.ehcache.annotations.aspectj;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.SuppressAjWarnings;
import org.aspectj.lang.reflect.MethodSignature;

import com.googlecode.ehcache.annotations.CacheAttributeSource;
import com.googlecode.ehcache.annotations.Cacheable;
import
com.googlecode.ehcache.annotations.interceptor.EhCacheInterceptor;

@SuppressWarnings("unused") // cacheable annotation
@Aspect
public class AnnotationEhCacheAspect {

private EhCacheInterceptor interceptor = new EhCacheInterceptor();
private CacheAttributeSource cacheAttributeSource;

@SuppressAjWarnings("adviceDidNotMatch")
@Around("execution(@Cacheable * * (..))")
public Object aroundCache(final ProceedingJoinPoint jp) throws
Throwable {
AspectJAopAllianceMethod m = new AspectJAopAllianceMethod(jp);
return interceptor.invoke(m);
}

protected CacheAttributeSource getCacheAttributeSource() {
return cacheAttributeSource;
}

public void setCacheAttributeSource(CacheAttributeSource
cacheAttributeSource) {
this.cacheAttributeSource = cacheAttributeSource;
interceptor.setCacheAttributeSource(cacheAttributeSource);
}

private static class AspectJAopAllianceMethod implements
MethodInvocation {
private ProceedingJoinPoint jp;

public AspectJAopAllianceMethod(ProceedingJoinPoint jp) {
super();
this.jp = jp;
}

public Object[] getArguments() {
return jp.getArgs();
}

public Object proceed() throws Throwable {
return jp.proceed();
}

public Object getThis() {
return jp.getThis();
}

public AccessibleObject getStaticPart() {
throw new UnsupportedOperationException("getStaticPart is not yet
supported");
}

public Method getMethod() {
MethodSignature methodSignature = (MethodSignature)
jp.getSignature();
return methodSignature.getMethod();

}

}
}

Adam Gent

unread,
May 17, 2011, 9:12:03 AM5/17/11
to Ehcache Spring Annotations
Eric and team I have gotten AspectJ to work very nicely with my
changes.
I tried to follow how spring is already using AspectJ as best I could.
The changes are here:
https://github.com/agentgt/ehcache-spring-annotations
I'll see if I can put together a patch from the github repo ( I
thought this would be easy but SVN doesn't like git patchs).

If you want to test it out there is a couple of things you might want
to do:
If you are using Eclipse I would install the AspectJ plugins then make
sure to add the
ehcache-spring-annotations-aspects to the aspect build path (not java
build path).

In your spring file you will have to change the XSD reference as I had
to make changes:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/
svn/schema/ehcache-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring

https://github.com/agentgt/ehcache-spring-annotations/raw/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.2.xsd">

<ehcache:annotation-driven cache-manager="ehCacheManager"
mode="aspectj"/>


The code is minimal but probably needs cleanup (you guys do not use
tabs but I do and so does Spring).
-Adam

Eric Dalquist

unread,
May 17, 2011, 11:08:57 AM5/17/11
to ehcache-sprin...@googlegroups.com
Awesome, thanks for the work! Nick and I are both going to be at a conference next week and we're spending this week doing prep so please don't be offended if we don't really get a chance to look at this until the start of June.

-Eric

Harshi

unread,
Jun 30, 2011, 10:18:27 PM6/30/11
to Ehcache Spring Annotations
Hi Adam,
Do you have sample configuration fro sprign 3.0? I am getting errors
on tomcat startup due to xsd errors from
https://raw.github.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.2.xsd


org.xml.sax.SAXParseException: schema_reference.4: Failed to read
schema document 'https://raw.github.com/agentgt/ehcache-spring-
annotations/master/core/src/main/resources/com/googlecode/ehcache/
annotations/ehcache-spring-1.2.xsd', because 1) could n
ot find the document; 2) the document could not be read; 3) the root
element of the document is not <xsd:schema>.
at
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:
195)
at
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(ErrorHandlerWrapper.java:
96)
at
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:
380)
at
com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:
318)
at
com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:
2541)
at
com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaWarning(XSDHandler.java:
2532)
at
com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:
1836)
at
com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:
531)
at
com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:
552)
at
com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:
2408)
at
com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:
1753)
at
com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.emptyElement(XMLSchemaValidator.java:
705)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:
377)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl
$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:
648)
at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:
140)
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:
510)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:
807)
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:
737)
> >http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-s...
>
> >https://github.com/agentgt/ehcache-spring-annotations/raw/master/core...
> > ">
>
> >    <ehcache:annotation-driven cache-manager="ehCacheManager"
> > mode="aspectj"/>
>
> > The code is minimal but probably needs cleanup (you guys do not use
> > tabs but I do and so does Spring).
> > -Adam
>
> > On May 17, 7:31 am, Adam Gent <adam.g...@evocatus.com> wrote:
> > > I am making rough attempt to get @Cacheable to work with AspectJ
> > > instead of proxies that I will post athttps://
> > github.com/agentgt/ehcache-spring-annotations- Hide quoted text -
>
> - Show quoted text -

Adam Gent

unread,
Jun 30, 2011, 11:46:19 PM6/30/11
to ehcache-sprin...@googlegroups.com
Harshi,
It looks like you have a network issue such that your tomcat cannot
download the XSD.
However most of the time Spring will use the XSD in the jar
(regardless of the web address) based on some XSD registration in
META-INF
so I am again surprised that you had the error.

Can you try running a unit test from eclipse or the command line (ant or maven).

-Adam

Harshi

unread,
Jul 1, 2011, 12:07:54 PM7/1/11
to Ehcache Spring Annotations
Thanks for responding Adam.

I have added schema to jar file and specified the path inside the jar
so spring picked up from there.

But my DAO method is still not looking at cache. Am I missing any
steps ? Please advice.

This is what I have in my DAO.

public Customer getCustomer(int customerId){

return loadCustmer(customerId) ;
}

@Cacheable
public Customer loadCustmer(int custerId){

JDBC code here.
}

On Jun 30, 8:46 pm, Adam Gent <adam.g...@evocatus.com> wrote:
> Harshi,
> It looks like you have a network issue such that your tomcat cannot
> download the XSD.
> However most of the time Spring will use the XSD in the jar
> (regardless of the web address) based on some XSD registration in
> META-INF
> so I am again surprised that you had the error.
>
> Can you try running a unit test from eclipse or the command line (ant or maven).
>
> -Adam
>
>
>
> On Thu, Jun 30, 2011 at 10:18 PM, Harshi <shn...@gmail.com> wrote:
> > Hi Adam,
> > Do you have sample configuration fro sprign 3.0? I am getting errors
> > on tomcat startup due to xsd errors from
> >https://raw.github.com/agentgt/ehcache-spring-annotations/master/core...
>
> > org.xml.sax.SAXParseException: schema_reference.4: Failed to read
> > schema document 'https://raw.github.com/agentgt/ehcache-spring-
> > annotations/master/core/src/main/resources/com/googlecode/ehcache/
> > annotations/ehcache-spring-1.2.xsd', because 1) could n
> > ot find the document; 2) the document could not be read; 3) the root
> > element of the document is not <xsd:schema>.
> >        at
> > com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseE­xception(ErrorHandlerWrapper.java:
> > 195)
> >        at
> > com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(ErrorHa­ndlerWrapper.java:
> > 96)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErr­orReporter.java:
> > 380)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErr­orReporter.java:
> > 318)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSche­maErr(XSDHandler.java:
> > 2541)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSche­maWarning(XSDHandler.java:
> > 2532)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaD­ocument(XSDHandler.java:
> > 1836)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchem­a(XSDHandler.java:
> > 531)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSc­hemaLoader.java:
> > 552)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGra­mmar(XMLSchemaValidator.java:
> > 2408)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartEl­ement(XMLSchemaValidator.java:
> > 1753)
> >        at
> > com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.emptyElement(­XMLSchemaValidator.java:
> > 705)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartE­lement(XMLNSDocumentScannerImpl.java:
> > 377)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl
> > $FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocu­mentScannerImpl.java:
> > 648)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNS­DocumentScannerImpl.java:
> > 140)
> >        at
> > com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scan­Document(XMLDocumentFragmentScannerImpl.java:
> > 510)
> >        at
> > com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Co­nfiguration.java:
> > 807)
> >        at
> > com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Co­nfiguration.java:
> >> - Show quoted text -- Hide quoted text -

Adam Gent

unread,
Jul 1, 2011, 2:47:55 PM7/1/11
to ehcache-sprin...@googlegroups.com
In the aspectj maven plugin you need to add it as a library:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
....
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations-aspects</artifactId>
</aspectLibrary>
....

If you are using Eclipse you also need to add the aspects jar to your
aspect path or else it will not weave the code.
You'll know if the code is weaved if in eclipse you see a red arrow on
the left hand side where the @Cacheable annotation is.

Or you can set a breakpoint in the method do a stackdump inside the method.
In the stack trace you should see some parent methods (methods calling
your @Cacheable method) that are aspectj autogenerated looking.

-Adam

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Harshi

unread,
Jul 4, 2011, 10:27:57 PM7/4/11
to Ehcache Spring Annotations
Hi Adam,

I am able to use this suing spring load time weaver but not CTW
( compile time weaver).

If you can send me exact steps and spring confuguration that would be
great.

Thanks
Harshi
Message has been deleted

Harshi

unread,
Jul 5, 2011, 5:28:47 PM7/5/11
to Ehcache Spring Annotations
Hi Adam,

I am able to use this suing spring load time weaver but not CTW
( compile time weaver).

If you can send me exact steps and spring confuguration that would be
great.

Thanks
Harshi



Harshi

unread,
Jul 5, 2011, 5:29:34 PM7/5/11
to Ehcache Spring Annotations
Hi Adam,

I finally got it working. Did you have to make changes to spring-
annotations jar besides creating spring-annotations-aspect jar ??


Thanks for your help.


Hi Eric and team, This would be excellent featute we should add it
to
main branch.


On Jul 4, 7:27 pm, Harshi <shn...@gmail.com> wrote:

Adam Gent

unread,
Jul 5, 2011, 8:02:17 PM7/5/11
to ehcache-sprin...@googlegroups.com
I'll see if I can setup an example project (pom.xml) with some sample code
to show the AspectJ work.

-Adam

Eric Dalquist

unread,
Jul 5, 2011, 8:49:24 PM7/5/11
to ehcache-sprin...@googlegroups.com
It is in the road map. Unfortunately Nick and I are also primary developers on another much larger open source project that is in RCs right now for an upcoming release so that is pretty much consuming every spare moment of my coding time right now. Rest assured that when we have some time to cycle back around baked in AspectJ support is on the list.

-Eric

Adam Gent

unread,
Jul 5, 2011, 10:28:19 PM7/5/11
to ehcache-sprin...@googlegroups.com
Eric if you like you can make me a temporary "commiter" just so that I
can put the code in
svn trunk (right now its in github). The risk of affecting other code
is low as I simply added a new child project (maven)
for the aspects (ehcache-spring-aspects) and changed some of the xml
bean factory code.

Once I'm done you can then remove my commit rights. Alternatively I
can try to send you a patch (which I thought would
be easy with git to svn but its not).

-Adam

Reply all
Reply to author
Forward
0 new messages