{"record":{"id":"88b17df79cdd1625","repo":"spring-projects/spring-framework","slug":"no-beanfactory-available-anymore-probably-due-to-88b17d","errorCode":null,"errorMessage":"No BeanFactory available anymore (probably due to serialization) - cannot resolve target with name '{}'","messagePattern":"No BeanFactory available anymore \\(probably due to serialization\\) - cannot resolve target with name '(.+?)'","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java","lineNumber":536,"sourceCode":"\t\t// We need to convert to an Advisor if necessary so that our source reference\n\t\t// matches what we find from superclass interceptors.\n\t\taddAdvisor(namedBeanToAdvisor(next));\n\t}\n\n\t/**\n\t * Return a TargetSource to use when creating a proxy. If the target was not\n\t * specified at the end of the interceptorNames list, the TargetSource will be\n\t * this class's TargetSource member. Otherwise, we get the target bean and wrap\n\t * it in a TargetSource if necessary.\n\t */\n\tprivate TargetSource freshTargetSource() {\n\t\tif (this.targetName == null) {\n\t\t\t// Not refreshing target: bean name not specified in 'interceptorNames'\n\t\t\treturn this.targetSource;\n\t\t}\n\t\telse {\n\t\t\tif (this.beanFactory == null) {\n\t\t\t\tthrow new IllegalStateException(\"No BeanFactory available anymore (probably due to serialization) \" +\n\t\t\t\t\t\t\"- cannot resolve target with name '\" + this.targetName + \"'\");\n\t\t\t}\n\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\tlogger.debug(\"Refreshing target with name '\" + this.targetName + \"'\");\n\t\t\t}\n\t\t\tObject target = this.beanFactory.getBean(this.targetName);\n\t\t\treturn (target instanceof TargetSource targetSource ? targetSource : new SingletonTargetSource(target));\n\t\t}\n\t}\n\n\t/**\n\t * Convert the following object sourced from calling getBean() on a name in the\n\t * interceptorNames array to an Advisor or TargetSource.\n\t */\n\tprivate Advisor namedBeanToAdvisor(Object next) {\n\t\ttry {\n\t\t\treturn this.advisorAdapterRegistry.wrap(next);\n\t\t}","sourceCodeStart":518,"sourceCodeEnd":554,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java#L518-L554","documentation":"Thrown as IllegalStateException by ProxyFactoryBean.freshTargetSource when targetName is set but this.beanFactory is null. The transient BeanFactory is required to resolve the named target bean. As with the other deserialization errors, the field is lost when the proxy crosses a serialization boundary.","triggerScenarios":"A ProxyFactoryBean with targetName set is deserialized, then a new instance (prototype mode or singleton re-cache via adviceChanged) is requested; freshTargetSource at lines 534-537 tries beanFactory.getBean(targetName) but beanFactory is null.","commonSituations":"Distributed session/cache serialization of AOP proxies whose target is a named bean; restart-from-snapshot; RMI/JMS transport of proxies.","solutions":["Do not serialize ProxyFactoryBean-derived proxies across JVMs; look the bean up by name after deserialization.","Inline the target object via setTarget instead of targetName if you must serialize (then no beanFactory lookup is needed).","Mark such beans transient/non-serializable and rebuild them from the ApplicationContext on the other side."],"exampleFix":"// before\n// (xml) <property name=\"targetName\" value=\"svc\"/>\nObject p = ctx.getBean(\"svcProxy\");\nsession.setAttribute(\"p\", p);\n\n// after\nsession.setAttribute(\"pName\", \"svcProxy\");\nObject p = ctx.getBean((String) session.getAttribute(\"pName\"));","handlingStrategy":"fallback","validationCode":"// If you must serialize, prefer setTarget over targetName so no factory lookup is needed\nif (pfb.getTargetName() != null && willBeSerialized) {\n  pfb.setTarget(beanFactory.getBean(pfb.getTargetName()));\n  pfb.setTargetName(null);\n}","typeGuard":"static boolean dependsOnBeanFactoryForTarget(ProxyFactoryBean fb) { return fb.getTargetName() != null; }","tryCatchPattern":"try { Object p = pfb.getObject(); }\ncatch (IllegalStateException e) {\n  if (e.getMessage().contains(\"cannot resolve target with name\")) { /* re-resolve by name */ }\n  throw e;\n}","preventionTips":["Avoid targetName for serializable proxies; use setTarget with the actual instance.","Store a bean-name key and call getBean(name) on the receiving JVM."],"tags":["spring-aop","proxyfactorybean","serialization","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}