spring+struct 提示找不到bean

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 >
注释掉以后,问题获得解决;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值