{"id":"d359d09f09848b09","repo":"spring-projects/spring-framework","slug":"cannot-use-prototypeaspectinstancefactory-with-bea","errorCode":null,"errorMessage":"Cannot use PrototypeAspectInstanceFactory with bean named '{}': 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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java#L32-L56","documentation":"Thrown by PrototypeAspectInstanceFactory's constructor when the named bean is not configured as a prototype in the BeanFactory (beanFactory.isPrototype(name) returns false). This factory is meant to produce a fresh aspect instance per invocation, which only makes sense for prototype-scoped beans; a singleton or unknown scope is rejected to enforce correct semantics.","triggerScenarios":"Constructing new PrototypeAspectInstanceFactory(beanFactory, beanName) where beanName refers to a singleton-scoped bean or a bean whose scope is not prototype. Spring constructs this internally for pertarget/perthis/pertypewithin aspects registered as beans.","commonSituations":"Declaring an aspect bean with scope=\"singleton\" (the default) but the aspect uses a perthis/pertarget per-clause that Spring routes through PrototypeAspectInstanceFactory. Misconfigured @Scope on an @Aspect bean. Manually instantiating PrototypeAspectInstanceFactory in tests with a singleton bean.","solutions":["Annotate the aspect bean with @Scope(\"prototype\") or set scope=\"prototype\" in XML/@Bean so beanFactory.isPrototype returns true.","If the aspect is genuinely singleton, remove the perthis/pertarget per-clause so Spring does not route it through PrototypeAspectInstanceFactory.","In tests, pass a mock BeanFactory whose isPrototype returns true for the name."],"exampleFix":"// before\n@Aspect(\"pertarget(execution(* com.acme..*(..)))\")\n@Component\npublic class PerTargetAuditAspect { ... }\n// after\n@Aspect(\"pertarget(execution(* com.acme..*(..)))\")\n@Component\n@Scope(\"prototype\")\npublic class PerTargetAuditAspect { ... }","handlingStrategy":"validation","validationCode":"import org.springframework.beans.factory.config.ConfigurableBeanFactory;\n\nvoid ensurePrototype(ConfigurableBeanFactory bf, String name) {\n  if (!bf.isPrototype(name))\n    throw new IllegalStateException(\"Bean '\" + name + \"' must be prototype-scoped for PrototypeAspectInstanceFactory\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mark perthis/pertarget/pertypewithin aspect beans as @Scope(\"prototype\").","Keep singleton aspects as plain @Aspect (no per-clause) so Spring avoids PrototypeAspectInstanceFactory.","In tests, stub BeanFactory.isPrototype to return true for the aspect name."],"tags":["spring-aop","aspectj","bean-scope","prototype","configuration"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}