spring-projects/spring-framework · error · IllegalArgumentException
Cannot use PrototypeAspectInstanceFactory with bean named '{
Error message
Cannot use PrototypeAspectInstanceFactory with bean named '{}': not a prototype What it means
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.
Source
Thrown at spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/PrototypeAspectInstanceFactory.java:50
* @author Juergen Hoeller
* @since 2.0
* @see org.springframework.beans.factory.BeanFactory
* @see LazySingletonAspectInstanceFactoryDecorator
*/
@SuppressWarnings("serial")
public class PrototypeAspectInstanceFactory extends BeanFactoryAspectInstanceFactory implements Serializable {
/**
* Create a PrototypeAspectInstanceFactory. AspectJ will be called to
* introspect to create AJType metadata using the type returned for the
* given bean name from the BeanFactory.
* @param beanFactory the BeanFactory to obtain instance(s) from
* @param name the name of the bean
*/
public PrototypeAspectInstanceFactory(BeanFactory beanFactory, String name) {
super(beanFactory, name);
if (!beanFactory.isPrototype(name)) {
throw new IllegalArgumentException(
"Cannot use PrototypeAspectInstanceFactory with bean named '" + name + "': not a prototype");
}
}
}
View on GitHub (pinned to e8729d0438)
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.
Example fix
// before
@Aspect("pertarget(execution(* com.acme..*(..)))")
@Component
public class PerTargetAuditAspect { ... }
// after
@Aspect("pertarget(execution(* com.acme..*(..)))")
@Component
@Scope("prototype")
public class PerTargetAuditAspect { ... } Defensive patterns
Strategy: validation
Validate before calling
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
void ensurePrototype(ConfigurableBeanFactory bf, String name) {
if (!bf.isPrototype(name))
throw new IllegalStateException("Bean '" + name + "' must be prototype-scoped for PrototypeAspectInstanceFactory");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- MethodInvocation is not a Spring ProxyMethodInvocation: {}
- 'argumentNames' property of AbstractAspectJAdvice contains a
- Only afterReturning advice can be used to bind a return valu
- Only afterThrowing advice can be used to bind a thrown excep
- Advice method [{}] requires {} arguments to be bound by name
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/d359d09f09848b09.json.
Report an issue: GitHub.