{"id":"387f172ec7ffe1b9","repo":"spring-projects/spring-framework","slug":"class-is-not-a-valid-aspect-type","errorCode":null,"errorMessage":"Class [{}] is not a valid aspect type","messagePattern":"Class \\[(.+?)\\] is not a valid aspect type","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java","lineNumber":136,"sourceCode":"\t * @see AspectJProxyUtils#makeAdvisorChainAspectJCapableIfNecessary(List)\n\t */\n\tprivate void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) {\n\t\tList<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory);\n\t\tClass<?> targetClass = getTargetClass();\n\t\tAssert.state(targetClass != null, \"Unresolvable target class\");\n\t\tadvisors = AopUtils.findAdvisorsThatCanApply(advisors, targetClass);\n\t\tAspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors);\n\t\tAnnotationAwareOrderComparator.sort(advisors);\n\t\taddAdvisors(advisors);\n\t}\n\n\t/**\n\t * Create an {@link AspectMetadata} instance for the supplied aspect type.\n\t */\n\tprivate AspectMetadata createAspectMetadata(Class<?> aspectClass, String aspectName) {\n\t\tAspectMetadata am = new AspectMetadata(aspectClass, aspectName);\n\t\tif (!am.getAjType().isAspect()) {\n\t\t\tthrow new IllegalArgumentException(\"Class [\" + aspectClass.getName() + \"] is not a valid aspect type\");\n\t\t}\n\t\treturn am;\n\t}\n\n\t/**\n\t * Create a {@link MetadataAwareAspectInstanceFactory} for the supplied aspect type. If the aspect type\n\t * has no per clause, then a {@link SingletonMetadataAwareAspectInstanceFactory} is returned, otherwise\n\t * a {@link PrototypeAspectInstanceFactory} is returned.\n\t */\n\tprivate MetadataAwareAspectInstanceFactory createAspectInstanceFactory(\n\t\t\tAspectMetadata am, Class<?> aspectClass, String aspectName) {\n\n\t\tMetadataAwareAspectInstanceFactory instanceFactory;\n\t\tif (am.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {\n\t\t\t// Create a shared aspect instance.\n\t\t\tObject instance = getSingletonAspectInstance(aspectClass);\n\t\t\tinstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(instance, aspectName);\n\t\t}","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java#L118-L154","documentation":"Thrown by AspectJProxyFactory.createAspectMetadata() when AjTypeSystem reports the class (and none of its superclasses up to Object) as not being an aspect. This means the class lacks a valid @Aspect annotation (or is an ajc-compiled aspect being ignored due to the spring.aop.ajc.ignore flag). Only classes recognized as AspectJ aspects can be added to an AspectJProxyFactory.","triggerScenarios":"Calling AspectJProxyFactory.addAspect(Class) or addAspect(Object) with a class that has no @Aspect annotation, or with an ajc-compiled aspect while spring.aop.ajc.ignore=true is set.","commonSituations":"Passing a plain advice bean (implementing MethodInterceptor etc.) instead of an @Aspect class. Forgetting the @Aspect annotation. Enabling spring.aop.ajc.ignore and then trying to add a code-style (ajc) aspect programmatically.","solutions":["Annotate the class with org.aspectj.lang.annotation.@Aspect.","If the class is ajc-compiled and spring.aop.ajc.ignore is true, either unset that flag or convert to annotation style.","For non-@Aspect advice (e.g. MethodInterceptor), use ProxyFactory.addAdvice() instead of AspectJProxyFactory.addAspect()."],"exampleFix":"// before\npublic class MyInterceptor { @Before(\"execution(* *(..))\") public void before() {} }\nfactory.addAspect(MyInterceptor.class);\n// after\n@Aspect\npublic class MyInterceptor { @Before(\"execution(* *(..))\") public void before() {} }\nfactory.addAspect(MyInterceptor.class);","handlingStrategy":"validation","validationCode":"import org.aspectj.lang.annotation.Aspect;\n\nvoid ensureAspect(Class<?> c) {\n  if (c.getAnnotation(Aspect.class) == null)\n    throw new IllegalArgumentException(c + \" must be annotated @Aspect to use with AspectJProxyFactory\");\n}","typeGuard":"boolean isAtAspect(Class<?> c) {\n  return c.isAnnotationPresent(org.aspectj.lang.annotation.Aspect.class);\n}","tryCatchPattern":null,"preventionTips":["Always annotate aspect classes with @Aspect.","For plain MethodInterceptor advice, use ProxyFactory.addAdvice() instead.","If using spring.aop.ajc.ignore, remember ajc-compiled aspects are excluded from isAspect()."],"tags":["spring-aop","aspectj","configuration","aspectj-proxy-factory"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}