{"id":"c305c6eeaf723c22","repo":"spring-projects/spring-framework","slug":"no-beanfactory-available-anymore-probably-due-to-c305c6","errorCode":null,"errorMessage":"No BeanFactory available anymore (probably due to serialization) - cannot resolve prototype advisor '{ppa.getBeanName()}'","messagePattern":"No BeanFactory available anymore \\(probably due to serialization\\) - cannot resolve prototype advisor '(.+?)'","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java","lineNumber":468,"sourceCode":"\t}\n\n\n\t/**\n\t * Return an independent advisor chain.\n\t * We need to do this every time a new prototype instance is returned,\n\t * to return distinct instances of prototype Advisors and Advices.\n\t */\n\tprivate List<Advisor> freshAdvisorChain() {\n\t\tAdvisor[] advisors = getAdvisors();\n\t\tList<Advisor> freshAdvisors = new ArrayList<>(advisors.length);\n\t\tfor (Advisor advisor : advisors) {\n\t\t\tif (advisor instanceof PrototypePlaceholderAdvisor ppa) {\n\t\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\t\tlogger.debug(\"Refreshing bean named '\" + ppa.getBeanName() + \"'\");\n\t\t\t\t}\n\t\t\t\t// Replace the placeholder with a fresh prototype instance resulting from a getBean lookup\n\t\t\t\tif (this.beanFactory == null) {\n\t\t\t\t\tthrow new IllegalStateException(\"No BeanFactory available anymore (probably due to \" +\n\t\t\t\t\t\t\t\"serialization) - cannot resolve prototype advisor '\" + ppa.getBeanName() + \"'\");\n\t\t\t\t}\n\t\t\t\tObject bean = this.beanFactory.getBean(ppa.getBeanName());\n\t\t\t\tAdvisor refreshedAdvisor = namedBeanToAdvisor(bean);\n\t\t\t\tfreshAdvisors.add(refreshedAdvisor);\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// Add the shared instance.\n\t\t\t\tfreshAdvisors.add(advisor);\n\t\t\t}\n\t\t}\n\t\treturn freshAdvisors;\n\t}\n\n\t/**\n\t * Add all global interceptors and pointcuts.\n\t */\n\tprivate void addGlobalAdvisors(ListableBeanFactory beanFactory, String prefix) {","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java#L450-L486","documentation":"freshAdvisorChain() replaces PrototypePlaceholderAdvisor entries (created for prototype-scope advice beans) with freshly resolved instances. If beanFactory is null at that point - which happens after Java serialization because the field is transient - the lookup cannot proceed and IllegalStateException is thrown. The message names the bean that could not be resolved.","triggerScenarios":"Serializing a ProxyFactoryBean whose prototype-scope advisor was deferred as a placeholder, then asking for a new prototype proxy instance (singleton=false) on the deserialized object; calling newPrototypeInstance() after the BeanFactory was cleared.","commonSituations":"Prototype ProxyFactoryBean stored in a session/distributed cache; remote session replication; mixing prototype advice with serialized proxies.","solutions":["Avoid serializing prototype-mode ProxyFactoryBean instances; re-fetch them from the context after deserialization.","Switch the advice bean to singleton scope if serialization is unavoidable (placeholders are only used for prototype advice).","Hold only the proxy in serialized state and refresh the FactoryBean from the ApplicationContext on the other side."],"exampleFix":"// before\n<bean class=\"...ProxyFactoryBean\" scope=\"prototype\">\n  <property name=\"interceptorNames\" value=\"myPrototypeAdvice\"/>\n</bean>\n// serialized across nodes\n\n// after: make the advice singleton so no placeholder is needed\n<bean id=\"myPrototypeAdvice\" class=\"...\" scope=\"singleton\"/>","handlingStrategy":"validation","validationCode":"// Guard against using a serialized prototype ProxyFactoryBean\nif (pfb.getBeanFactory() == null && !pfb.isSingleton()) {\n    throw new IllegalStateException(\"Refusing prototype proxy creation: BeanFactory missing (serialized?).\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not serialize prototype ProxyFactoryBean instances.","Make advice beans singleton scope if serialization is unavoidable.","Re-resolve prototype proxies from the context after deserialization."],"tags":["aop","serialization","prototype","proxy-factory-bean"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}