2013年5月9日 星期四

Spring Cache AspectJ Setup

1. In pom.xml
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-instrument</artifactid>
  <version>${org.springframework-version}</version>
</dependency>
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-aspects</artifactid>
  <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>org.aspectj</groupid>
 <artifactid>aspectjrt</artifactid>
 <version>1.7.2</version>
</dependency> 

2. In spring-context.xml
<context:load-time-weaver/>
<cache:annotation-driven mode="aspectj" />

3. Add JVM variable
 -javaagent:spring-instrument-3.x.x.RELEASE.jar
 (The file can be found in the maven directory)

4. Options: custom aop.xml
You may not want AspectJ try to weave all classes. Put your own aop.xml under META-INF like below

Look for more detail

<?xml version="1.0"?>

<!--
 AspectJ load-time weaving config file to install common Spring aspects.
-->

<aspectj>

<!-- Only weave the classes in willy.package -->
    <weaver options="-verbose -showWeaveInfo">
        <include within="willy.package.*" />
    </weaver>
    
    <!-- Below are the annotations used by spring-aspects -->
    <aspects>
        <aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" />
        <aspect name="org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect" />
        <aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect" />
        <aspect name="org.springframework.cache.aspectj.AnnotationCacheAspect" />
    </aspects>
</aspectj>



沒有留言:

張貼留言