{"record":{"id":"2670ce9c7891b7b1","repo":"spring-projects/spring-framework","slug":"cannot-use-prototypeaspectinstancefactory-with-bea","errorCode":null,"errorMessage":"Cannot use PrototypeAspectInstanceFactory with bean named '{name}': not a prototype","messagePattern":"Cannot use PrototypeAspectInstanceFactory with bean named '(.+?)': not a prototype","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java","lineNumber":50,"sourceCode":" * @author Juergen Hoeller\n * @since 2.0\n * @see org.springframework.beans.factory.BeanFactory\n * @see LazySingletonAspectInstanceFactoryDecorator\n */\n@SuppressWarnings(\"serial\")\npublic class PrototypeAspectInstanceFactory extends BeanFactoryAspectInstanceFactory implements Serializable {\n\n\t/**\n\t * Create a PrototypeAspectInstanceFactory. AspectJ will be called to\n\t * introspect to create AJType metadata using the type returned for the\n\t * given bean name from the BeanFactory.\n\t * @param beanFactory the BeanFactory to obtain instance(s) from\n\t * @param name the name of the bean\n\t */\n\tpublic PrototypeAspectInstanceFactory(BeanFactory beanFactory, String name) {\n\t\tsuper(beanFactory, name);\n\t\tif (!beanFactory.isPrototype(name)) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Cannot use PrototypeAspectInstanceFactory with bean named '\" + name + \"': not a prototype\");\n\t\t}\n\t}\n\n}\n","sourceCodeStart":32,"sourceCodeEnd":56,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java#L32-L56","documentation":"Thrown as an IllegalArgumentException from the PrototypeAspectInstanceFactory constructor when the bean named in the BeanFactory is not a prototype-scoped bean (beanFactory.isPrototype(name) returns false). This factory is designed to back perthis/pertarget aspects that must yield a fresh instance each time; using it against a singleton bean would violate those semantics, so Spring rejects the mismatch at construction.","triggerScenarios":"Constructing new PrototypeAspectInstanceFactory(beanFactory, name) where the bean 'name' is singleton-scoped; BeanFactoryAspectJAdvisorsBuilder internally creating a PrototypeAspectInstanceFactory for a perthis/pertarget aspect whose bean scope was not set to prototype (companion to error 52).","commonSituations":"Directly instantiating PrototypeAspectInstanceFactory in framework code with a singleton bean; a misconfigured aspect where @Scope(\"prototype\") was omitted or overridden; XML wiring pointing the factory at a singleton bean.","solutions":["Set the bean's scope to prototype: add @Scope(\"prototype\") or scope=\"prototype\" in XML.","If the aspect is genuinely singleton, do not use PrototypeAspectInstanceFactory; use SingletonMetadataAwareAspectInstanceFactory instead.","Verify the bean name passed to the constructor resolves to the intended prototype bean."],"exampleFix":"// before\n@Bean\n@Aspect(\"perthis(execution(* com.example.Target.*(..)))\")\npublic static PerTargetAspect perTargetAspect() { return new PerTargetAspect(); }\n// PrototypeAspectInstanceFactory(beanFactory, \"perTargetAspect\") throws (singleton)\n\n// after\n@Bean\n@Scope(\"prototype\")\n@Aspect(\"perthis(execution(* com.example.Target.*(..)))\")\npublic static PerTargetAspect perTargetAspect() { return new PerTargetAspect(); }","handlingStrategy":"validation","validationCode":"import org.springframework.beans.factory.BeanFactory;\n\npublic static boolean isPrototype(BeanFactory bf, String name) {\n    return bf.containsBean(name) && bf.isPrototype(name);\n}\n\n// before new PrototypeAspectInstanceFactory(bf, name):\nif (!isPrototype(bf, name)) {\n    throw new IllegalStateException(\"Bean '\" + name + \"' must be prototype-scoped\");\n}","typeGuard":"import org.springframework.beans.factory.BeanFactory;\n\npublic static boolean canUsePrototypeAspectFactory(BeanFactory bf, String name) {\n    return bf.containsBean(name) && bf.isPrototype(name);\n}","tryCatchPattern":"try {\n    new PrototypeAspectInstanceFactory(beanFactory, name);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().contains(\"not a prototype\")) {\n        // set the bean scope to prototype or use SingletonMetadataAwareAspectInstanceFactory\n    } else throw ex;\n}","preventionTips":["Pair PrototypeAspectInstanceFactory only with prototype-scoped beans.","For singleton aspects, use SingletonMetadataAwareAspectInstanceFactory instead.","Validate bean scope programmatically before constructing the factory."],"tags":["spring-aop","aspectj","bean-scope","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}