title: Spring Dependency Injecting an annotated Aspect
body: <p>Using Spring I've had some issues with doing a dependency injection on an annotated Aspect class.  CacheService is injected upon the Spring context's startup, but when the weaving takes place, it says that the cacheService is null.  So I am forced to relook up the spring context manually and get the bean from there.
  Is there another way of going about it?</p>
  
  <p>Here is an example of my Aspect:</p>
  
  <pre><code>import org.apache.log4j.Logger;
  import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.annotation.Around;
  import org.aspectj.lang.annotation.Aspect;
  import com.mzgubin.application.cache.CacheService;
  
  @Aspect
  public class CachingAdvice {
  
    private static Logger log = Logger.getLogger(CachingAdvice.class);
  
    private CacheService cacheService;
  
    @Around("execution(public *com.mzgubin.application.callMethod(..)) &amp;&amp;"
              + "args(params)")
      public Object addCachingToCreateXMLFromSite(ProceedingJoinPoint pjp, InterestingParams params) throws Throwable {
      log.debug("Weaving a method call to see if we should return something from the cache or create it from scratch by letting control flow move on");
  
      Object result = null;
      if (getCacheService().objectExists(params))}{
        result = getCacheService().getObject(params);
      } else {
        result = pjp.proceed(pjp.getArgs());
        getCacheService().storeObject(params, result);
      }
      return result;
    }
  
    public CacheService getCacheService(){
      return cacheService;
    }
  
    public void setCacheService(CacheService cacheService){
      this.cacheService = cacheService;
    }
  }
  </code></pre>