{"record":{"id":"c9e5998610a6c79f","repo":"spring-projects/spring-framework","slug":"cannot-apply-lookup-to-beans-without-correspondin","errorCode":null,"errorMessage":"Cannot apply @Lookup to beans without corresponding bean definition","messagePattern":"Cannot apply @Lookup to beans without corresponding bean definition","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java","lineNumber":471,"sourceCode":"\n\tprivate void checkLookupMethods(Class<?> beanClass, final String beanName) throws BeanCreationException {\n\t\tif (!this.lookupMethodsChecked.contains(beanName)) {\n\t\t\tif (AnnotationUtils.isCandidateClass(beanClass, Lookup.class)) {\n\t\t\t\ttry {\n\t\t\t\t\tClass<?> targetClass = beanClass;\n\t\t\t\t\tdo {\n\t\t\t\t\t\tReflectionUtils.doWithLocalMethods(targetClass, method -> {\n\t\t\t\t\t\t\tLookup lookup = method.getAnnotation(Lookup.class);\n\t\t\t\t\t\t\tif (lookup != null) {\n\t\t\t\t\t\t\t\tAssert.state(this.beanFactory != null, \"No BeanFactory available\");\n\t\t\t\t\t\t\t\tLookupOverride override = new LookupOverride(method, lookup.value());\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tRootBeanDefinition mbd = (RootBeanDefinition)\n\t\t\t\t\t\t\t\t\t\t\tthis.beanFactory.getMergedBeanDefinition(beanName);\n\t\t\t\t\t\t\t\t\tmbd.getMethodOverrides().addOverride(override);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcatch (NoSuchBeanDefinitionException ex) {\n\t\t\t\t\t\t\t\t\tthrow new BeanCreationException(beanName,\n\t\t\t\t\t\t\t\t\t\t\t\"Cannot apply @Lookup to beans without corresponding bean definition\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\ttargetClass = targetClass.getSuperclass();\n\t\t\t\t\t}\n\t\t\t\t\twhile (targetClass != null && targetClass != Object.class);\n\n\t\t\t\t}\n\t\t\t\tcatch (IllegalStateException ex) {\n\t\t\t\t\tthrow new BeanCreationException(beanName, \"Lookup method resolution failed\", ex);\n\t\t\t\t}\n\t\t\t}\n\t\t\tthis.lookupMethodsChecked.add(beanName);\n\t\t}\n\t}\n\n\t@Override","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java#L453-L489","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the bean with @Lookup methods is declared as a proper bean (via @Component, @Bean, or XML <bean>) so it has a BeanDefinition.","Obtain the bean instance from the ApplicationContext rather than instantiating it with new.","If using registerSingleton, switch to registerBeanDefinition or declare the bean via @Bean instead."],"exampleFix":"// before\nMyService service = new MyService();\n// @Lookup method on MyService fails\n\n// after\nMyService service = context.getBean(MyService.class);\n// or declare as @Component so it has a bean definition","handlingStrategy":"validation","validationCode":"// Verify the bean has a definition before relying on @Lookup\nif (beanFactory.containsBeanDefinition(beanName)) {\n    // safe — @Lookup can be applied\n} else {\n    // register the bean definition first\n}","typeGuard":null,"tryCatchPattern":"try {\n    context.getBean(MyService.class);\n} catch (BeanCreationException ex) {\n    if (ex.getMessage().contains(\"@Lookup\")) {\n        // bean lacks a definition — register it properly\n    }\n}","preventionTips":["Always obtain @Lookup-containing beans from the ApplicationContext, never via new.","Declare @Lookup beans via @Component or @Bean so they have a BeanDefinition.","Avoid registerSingleton for beans that use @Lookup — use registerBeanDefinition instead."],"tags":["spring","lookup","bean-definition","di"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}