這裏是我的示例類:AspectJ的表達式「@within」不混凝土類效應不具有公共成員
public abstract class AbstractAgent {
public void count(List<Movie> movies) {
sum(movies);
}
protected abstract void sum(List<Movie> movies);
}
@Broker
public class DefaultAgent extends AbstractAgent {
@Override
protected void sum(List<Movie> movies) {
Validate.notNull(movies);
}
}
方面定義:
@Aspect
@Component
public class DaoObserver {
@Pointcut("@within(source.service.Broker)")
public void withinBroker() {
}
@AfterReturning("withinBroker()")
public void alertBroker(JoinPoint jp) {
System.out.println("Cached broker execution of {"
+ jp.getSignature().toShortString() + "}");
}
}
我發現實例的DefaultAgent沒有被代理!
試過線如下:
applicationContext.getBeansOfType(AbstractAgent.class);
建議我能找到像 'DefaultAgent $$ EnhancerByCGLIB $$ ae10cb14',但仍 'DefaultAgent'。
然後我發現它會工作,如果我添加一個公共方法類'DefaultAgent'。
挖進一步我發現根本原因是在AspectJ織入:
org.aspectj.weaver.patterns.WithinAnnotationPointcut.matchInternal(暗影)
@Override
protected FuzzyBoolean matchInternal(Shadow shadow) {
ResolvedType enclosingType = shadow.getIWorld().resolve(shadow.getEnclosingType(), true);
if (enclosingType.isMissing()) {
shadow.getIWorld().getLint().cantFindType.signal(new String[] { WeaverMessages.format(
WeaverMessages.CANT_FIND_TYPE_WITHINPCD, shadow.getEnclosingType().getName()) }, shadow.getSourceLocation(),
new ISourceLocation[] { getSourceLocation() });
}
annotationTypePattern.resolve(shadow.getIWorld());
return annotationTypePattern.matches(enclosingType); **<--- AbstractAgent**
}
是否AspectJ織入的錯誤?我該如何解決這個問題,因爲我在實際業務中有許多具體的子類,它是模板'Template'的標準實現。