{"id":"f2846912df5c27ef","repo":"spring-projects/spring-framework","slug":"no-beanfactory-available-anymore-probably-due-to-f28469","errorCode":null,"errorMessage":"No BeanFactory available anymore (probably due to serialization) - cannot resolve target with name '{targetName}'","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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java#L518-L554","documentation":"freshTargetSource() must call getBean(targetName) to resolve the target. If beanFactory is null (transient field lost to serialization), it cannot resolve and throws IllegalStateException naming targetName. This path is taken only when the target was supplied via the trailing interceptorNames entry (stored into targetName) rather than as an explicit target/targetSource property.","triggerScenarios":"ProxyFactoryBean configured with target as the last interceptorNames entry, then serialized, then asked for a fresh target source (e.g. prototype proxy instantiation) on the deserialized instance.","commonSituations":"Session replication / distributed caching of prototype ProxyFactoryBean whose target is named in interceptorNames; remoting scenarios where the bean factory is not reattached.","solutions":["Set target explicitly via setTarget/setTargetSource instead of via the trailing interceptorNames entry so it is carried as serializable state.","Do not serialize prototype ProxyFactoryBean; re-resolve from the context after deserialization.","Use singleton scope to avoid freshTargetSource() being invoked."],"exampleFix":"// before\n<property name=\"interceptorNames\" value=\"myAdvice,myTarget\"/>\n\n// after\n<property name=\"interceptorNames\" value=\"myAdvice\"/>\n<property name=\"targetName\" value=\"myTarget\"/>\n// or set target directly: <property name=\"target\" ref=\"myTarget\"/>","handlingStrategy":"validation","validationCode":"if (pfb.getBeanFactory() == null && pfb.getTargetName() != null) {\n    throw new IllegalStateException(\"Cannot resolve target by name post-serialization; set target explicitly.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer setting target/targetSource explicitly over relying on the trailing interceptorNames entry.","Avoid serializing prototype ProxyFactoryBean that reference targets by name.","Re-resolve proxies from the ApplicationContext after deserialization."],"tags":["aop","serialization","target","proxy-factory-bean"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}