gpt4 book ai didi

java - Spring 框架 : No BeanFactory available anymore (probably due to serialization)

转载 作者:行者123 更新时间:2023-12-04 04:51:33 24 4
gpt4 key购买 nike

我正在尝试将 xml 配置的 bean 转换为 JavaConfig。xml 版本可以正常工作,但在使用 JavaConfig 版本时我不断收到错误消息:

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)

工作 xml 配置:

<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>

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();
}
}

最佳答案

您正在创建一个新的 ProxyFactoryBean,无需 Spring 的帮助。 ProxyFactoryBean 需要一个通过 setBeanFactory 注入(inject)的 BeanFactory。实际上 ProxyFactoryBean 实现了 BeanFactoryAware。这意味着,当 Spring 创建实例时,它会自动注入(inject) FactoryBean。您必须自己使用 Java 配置来管理它。但是我认为 XML 或基于注释的是配置 Spring 的标准方式。这里为什么要把它转换为基于Java的配置?

关于java - Spring 框架 : No BeanFactory available anymore (probably due to serialization),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com