Johan Frick
Johan Frick

Reputation: 1124

Spring Framework: No BeanFactory available anymore (probably due to serialization)

I am trying to convert an xml configured bean to JavaConfig. The xml version is working, but I keep getting error when using the JavaConfig version:

Caused by: java.lang.IllegalStateException: No BeanFactory available anymore (probably due to serialization) - cannot resolve interceptor names [cacheAdvisor]
at org.springframework.aop.framework.ProxyFactoryBean.initializeAdvisorChain(ProxyFactoryBean.java:423)

Working xml configuration:

<bean id="contentLogic" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="interceptorNames">
        <list>
            <value>cacheAdvisor</value>
        </list>
    </property>
    <property name="proxyInterfaces" value="com.company.logic.ContentLogic"/>
    <property name="target">
        <bean class="com.company.logic.ContentLogicImpl"/>
    </property> 
</bean>

Not working JavaConfig:

@Configuration
public class SpringConfiguration {

    @Bean
    public ContentLogic getRealContentLogic() throws ClassNotFoundException {
        ProxyFactoryBean factory = new ProxyFactoryBean();
        factory.setInterceptorNames(new String[]{"cacheAdvisor"});
        factory.setTargetClass(ContentLogicImpl.class);
        factory.setProxyInterfaces(new Class[]{ContentLogic.class});
        return (ContentLogic) factory.getObject();
    }
}

Upvotes: 3

Views: 2199

Answers (1)

LaurentG
LaurentG

Reputation: 11757

You are creating a new ProxyFactoryBean yourself without help of Spring. ProxyFactoryBean needs a BeanFactory which is injected through setBeanFactory. Actually ProxyFactoryBean implements BeanFactoryAware. That means, when Spring creates the instance, it automatically inject the FactoryBean. You would have to manage this yourself with the Java configuration. However I think the XML or annotation-based is more the standard way to config Spring. Why do you want here to convert it to Java-based config?

Upvotes: 4

Related Questions