spring-projects/spring-framework · error · IllegalArgumentException
Property 'targetBeanName' is required
Error message
Property 'targetBeanName' is required
What it means
MethodLocatingFactoryBean is a FactoryBean that resolves a java.lang.reflect.Method from a target bean during BeanFactory initialization. The 'targetBeanName' property names which bean to introspect; without it there is no class to look the method up on. The exception is thrown from setBeanFactory() (the BeanFactoryAware callback, line 64) so the failure surfaces at application-context startup.
Solutions
- Add <property name="targetBeanName" value="myServiceBean"/> (XML) or call factoryBean.setTargetBeanName("myServiceBean") so the bean has a non-blank name
- Confirm the bean name you supply matches an actual bean id/name in the same ApplicationContext
- Run a quick grep on the config for MethodLocatingFactoryBean and verify every occurrence sets both targetBeanName and methodName
Example fix
// before <bean id="myMethod" class="org.springframework.aop.config.MethodLocatingFactoryBean"> <property name="methodName" value="doWork"/> </bean> // after <bean id="myMethod" class="org.springframework.aop.config.MethodLocatingFactoryBean"> <property name="targetBeanName" value="myService"/> <property name="methodName" value="doWork"/> </bean>
Defensive patterns
Strategy: validation
Validate before calling
// Before relying on the factory bean, ensure the property is set:
if (!StringUtils.hasText(factoryBean.getTargetBeanName())) {
throw new IllegalStateException("targetBeanName not set on MethodLocatingFactoryBean");
} Type guard
private static boolean hasTargetBeanName(MethodLocatingFactoryBean fb) {
return fb != null && StringUtils.hasText(fb.getTargetBeanName());
} Prevention
- Centralize MethodLocatingFactoryBean creation in a helper that requires both targetBeanName and methodName as parameters
- Add a unit test that loads the relevant application context to catch missing properties at build time
When it happens
Trigger: Defining a MethodLocatingFactoryBean bean (XML, Java @Bean, or GenericBeanDefinition) and never calling setTargetBeanName(...) before the context initializes it. The check is !StringUtils.hasText(this.targetBeanName), so null, empty, or whitespace-only all trip it.
Common situations: XML <bean class="...MethodLocatingFactoryBean"> missing the <property name="targetBeanName" .../> element; a typo in the property name; a refactored config that accidentally dropped the setter call; copy-pasting a bean definition without the required properties.
Related errors
- Property 'methodName' is required
- Can't determine type of bean with name
- Property 'target' is required
- Unable to locate method
- Both 'constructorArgs' and 'constructorArgTypes' need to be…
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/1c25e48f1486e173.
Report an issue: GitHub.
Appendix: source
Thrown at spring-aop/src/main/java/org/springframework/aop/config/MethodLocatingFactoryBean.java:65
* @param targetBeanName the name of the bean to locate the {@link Method} on
*/
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = targetBeanName;
}
/**
* Set the name of the {@link Method} to locate.
* <p>This property is required.
* @param methodName the name of the {@link Method} to locate
*/
public void setMethodName(String methodName) {
this.methodName = methodName;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!StringUtils.hasText(this.targetBeanName)) {
throw new IllegalArgumentException("Property 'targetBeanName' is required");
}
if (!StringUtils.hasText(this.methodName)) {
throw new IllegalArgumentException("Property 'methodName' is required");
}
Class<?> beanClass = beanFactory.getType(this.targetBeanName);
if (beanClass == null) {
throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'");
}
this.method = BeanUtils.resolveSignature(this.methodName, beanClass);
if (this.method == null) {
throw new IllegalArgumentException("Unable to locate method [" + this.methodName +
"] on bean [" + this.targetBeanName + "]");
}
}
View on GitHub (pinned to 69bf83ad71)