我有一个根上下文文件 (applicationContext.xml) 和一个子上下文文件 (subContext.xml)。
当我尝试从 subContext.xml 中的 applicationContext.xml 访问 bean 时出现 NoSuchBeanDefinitionException
subContext.xml 豆:
<bean id="myInfo" factory-bean="myInfoFactory"
factory-method="getInstance" scope="request">
<aop:scoped-proxy proxy-target-class="true" />
applicationContext.xml 豆:
<bean id="myInfoFactory"
class="org.wcw.factory.MyInfoFactory" />
我得到的错误是
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myInfoFactory' is defined
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
portlet.xml
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/subContext.xml</value>
</init-param>
如果我将 myInfoFactory bean 声明从 applicationContext.xml 移动到 subContext.xml,它就不会再抛出该异常了。不确定为什么在 applicationContext.xml 中声明它是不可见的。由于我的环境,我无法将 bean 永久移动到 subContext.xml。MyInfoFactory 类
public class MyInfoFactory {
private Logger log;
@Autowired
private PortletRequest request;
...
public MyInfo getInstance(PortletRequest request) {
}
...
}