{"record":{"id":"feacc5a5941826f4","repo":"spring-projects/spring-framework","slug":"class-aspectclass-getname-is-not-a-valid-asp","errorCode":null,"errorMessage":"Class [{aspectClass.getName()}] 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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java#L118-L154","documentation":"Thrown as an IllegalArgumentException from AspectJProxyFactory.createAspectMetadata() when AspectMetadata construction completes but ajType.isAspect() is false. AspectMetadata walks the class hierarchy looking for an aspect; if none is found it would already throw, so this guard primarily catches edge cases (e.g., an AjType that exists but isAspect() reports false at re-check). Practically it means the class passed to addAspect(...) is not an @AspectJ aspect.","triggerScenarios":"Calling AspectJProxyFactory.addAspect(Object) or addAspect(Class) with a class that lacks @Aspect; passing an interface or a plain POJO annotated only with advice annotations (@Before, @Around) but not @Aspect.","commonSituations":"Forgetting @Aspect on a class passed to the programmatic proxy factory; passing the target class instead of the aspect class by mistake; refactoring that removed @Aspect.","solutions":["Annotate the class with @org.aspectj.lang.annotation.Aspect.","Double-check you are passing the aspect class/instance, not the target class.","Ensure the correct class (the one declaring @Before/@Around methods) is supplied to addAspect."],"exampleFix":"// before\nAspectJProxyFactory factory = new AspectJProxyFactory(service);\nfactory.addAspect(LoggingHandler.class); // LoggingHandler has no @Aspect\n// after\n@Aspect\npublic class LoggingHandler {\n    @Before(\"execution(* com.example.*.*(..))\")\n    public void log() { ... }\n}\n\nAspectJProxyFactory factory = new AspectJProxyFactory(service);\nfactory.addAspect(LoggingHandler.class);","handlingStrategy":"validation","validationCode":"import org.aspectj.lang.annotation.Aspect;\nimport org.springframework.core.annotation.AnnotationUtils;\n\npublic static boolean isValidAspectType(Class<?> clazz) {\n    return AnnotationUtils.findAnnotation(clazz, Aspect.class) != null;\n}\n\n// before AspectJProxyFactory.addAspect(clazz):\nif (!isValidAspectType(clazz)) {\n    throw new IllegalArgumentException(clazz + \" is not an @AspectJ aspect\");\n}","typeGuard":"import org.aspectj.lang.annotation.Aspect;\nimport org.springframework.core.annotation.AnnotationUtils;\n\npublic static boolean isAspectType(Class<?> c) {\n    return AnnotationUtils.findAnnotation(c, Aspect.class) != null;\n}","tryCatchPattern":"try {\n    factory.addAspect(candidateClass);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().contains(\"not a valid aspect type\")) {\n        // annotate the class with @Aspect or pick the correct class\n    } else throw ex;\n}","preventionTips":["Verify @Aspect is present before calling addAspect; pass the aspect class, not the target.","Keep aspect classes and target classes in distinct packages to avoid confusion.","Add a compile-time or test check that aspect classes carry @Aspect."],"tags":["spring-aop","aspectj","configuration","programmatic-proxy"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}