Spring Dependency Injetando um Aspecto Anotado
Usando o Spring, tive alguns problemas ao fazer uma injeção de dependência em uma classe Aspect anotada. CacheService é injetado na inicialização do contexto Spring, mas quando ocorre a tecelagem, ele diz que o cacheService é nulo. Então, sou forçado a recolocar manualmente o contexto da mola e obter o bean a partir daí. Existe outra maneira de fazer isso?
Aqui está um exemplo do meu aspecto:
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(..)) &&"
+ "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;
}
}