spring+struct 提示找不到bean

来源:互联网 发布:无线网络管理器软件 编辑:程序博客网 时间:2024/06/03 18:41

spring+structs配置不当导致无法正常获取bean

异常如下:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userProfileMananger' is definedorg.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:360)org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:692)org.springframework.beans.factory.support.Abstra

web.xml文件配置如下

 

<context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
  
</context-param>
  
<listener>
      
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  
</listener>
  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    
<init-param>
      
<param-name>config</param-name>
      
<param-value>/WEB-INF/struts-config.xml</param-value>
    
</init-param>
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>3</param-value>
    
</init-param>
    
<init-param>
      
<param-name>detail</param-name>
      
<param-value>3</param-value>
    
</init-param>
    
<load-on-startup>0</load-on-startup>
  
</servlet>
  
<servlet-mapping>
    
<servlet-name>action</servlet-name>
    
<url-pattern>*.do</url-pattern>
  
</servlet-mapping>


structs-config.xml配置如下

 

<struts-config>
  
<data-sources />
  
<form-beans />
  
<global-exceptions />
  
<global-forwards />
  
<action-mappings >
    
<action path="/searchUserProfiles" type="com.homecounter.web.action.UserProfilesAction">
      
<forward name="success" path="/WEB-INF/pages/listAll.jsp" />
    
</action>
    

  
</action-mappings>

  
<message-resources parameter="com.homecounter.web.ApplicationResources" />
  
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn" >
      
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext-*.xml"/>       
  
</plug-in>
</struts-config>

调用

WebApplicationContext wac = this.getWebApplicationContext();

UserProfileManager profileMan = (UserProfileManager)wac.getBean("userProfileMananger");

使用如上方法获取bean userProfileMananger 时,出现文章开头的异常;但是使用如下代码

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("E://workspace//HomeCounter//WebRoot//WEB-INF//applicationContext.xml"));
  
UserProfileManager profileMan = (UserProfileManager)wac.getBean("userProfileMananger");

获取bean 时,没有任何异常;

原因及解决:

structs中是用spring的bean的时候,需要在structs_config.xml文件中配置plug-in,如下

 

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn" >
   
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext-*.xml"/>    
 
</plug-in>

但是我在web.xml中作了如下配置

 

<context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
  
</context-param>

导致了/WEB-INF/applicationContext-*.xml中的bean被重复的装载和初始化,因此导致了问题的出现;

通过将

<context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
  
</context-param>
注释掉以后,问题获得解决;