spring-projects/spring-framework · error · BeanCreationException

Cannot apply @Lookup to beans without corresponding bean…

Error message

Cannot apply @Lookup to beans without corresponding bean definition

What it means

Thrown inside checkLookupMethods() when a method annotated with @Lookup is found on a bean whose name has no registered BeanDefinition in the BeanFactory. @Lookup works by registering a LookupOverride on the bean's RootBeanDefinition; if getMergedBeanDefinition(beanName) throws NoSuchBeanDefinitionException, the override cannot be applied and the bean creation fails.

Solutions

  1. Ensure the bean with @Lookup methods is declared as a proper bean (via @Component, @Bean, or XML <bean>) so it has a BeanDefinition.
  2. Obtain the bean instance from the ApplicationContext rather than instantiating it with new.
  3. If using registerSingleton, switch to registerBeanDefinition or declare the bean via @Bean instead.

Example fix

// before
MyService service = new MyService();
// @Lookup method on MyService fails

// after
MyService service = context.getBean(MyService.class);
// or declare as @Component so it has a bean definition
Defensive patterns

Strategy: validation

Validate before calling

// Verify the bean has a definition before relying on @Lookup
if (beanFactory.containsBeanDefinition(beanName)) {
    // safe — @Lookup can be applied
} else {
    // register the bean definition first
}

Try / catch

try {
    context.getBean(MyService.class);
} catch (BeanCreationException ex) {
    if (ex.getMessage().contains("@Lookup")) {
        // bean lacks a definition — register it properly
    }
}

Prevention

When it happens

Trigger: A @Lookup-annotated method exists on a class, but the bean is not registered as a proper bean definition (e.g., it was instantiated manually via new and processed by processInjection, or registered only as a singleton without a definition). Also occurs when @Lookup is on a prototype bean whose definition was removed or never created.

Common situations: Using @Lookup on an object created via new rather than obtained from the context. Registering a bean via SingletonBeanRegistry.registerSingleton (which does not create a BeanDefinition) and then expecting @Lookup to work. Misconfiguring a bean scope so the definition is missing at injection time.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/c9e5998610a6c79f. Report an issue: GitHub.

Appendix: source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java:471

	private void checkLookupMethods(Class<?> beanClass, final String beanName) throws BeanCreationException {
		if (!this.lookupMethodsChecked.contains(beanName)) {
			if (AnnotationUtils.isCandidateClass(beanClass, Lookup.class)) {
				try {
					Class<?> targetClass = beanClass;
					do {
						ReflectionUtils.doWithLocalMethods(targetClass, method -> {
							Lookup lookup = method.getAnnotation(Lookup.class);
							if (lookup != null) {
								Assert.state(this.beanFactory != null, "No BeanFactory available");
								LookupOverride override = new LookupOverride(method, lookup.value());
								try {
									RootBeanDefinition mbd = (RootBeanDefinition)
											this.beanFactory.getMergedBeanDefinition(beanName);
									mbd.getMethodOverrides().addOverride(override);
								}
								catch (NoSuchBeanDefinitionException ex) {
									throw new BeanCreationException(beanName,
											"Cannot apply @Lookup to beans without corresponding bean definition");
								}
							}
						});
						targetClass = targetClass.getSuperclass();
					}
					while (targetClass != null && targetClass != Object.class);

				}
				catch (IllegalStateException ex) {
					throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
				}
			}
			this.lookupMethodsChecked.add(beanName);
		}
	}

	@Override

View on GitHub (pinned to 69bf83ad71)