当前位置: 代码迷 >> Java Web开发 >> 为什么在spring中实例化bean无效
  详细解决方案

为什么在spring中实例化bean无效

热度:83   发布时间:2016-04-16 21:39:53.0
为何在spring中实例化bean无效?
源码如下:
struts 文件中:
 <include file="/com/uy/conf/ui-confmgmt.xml"></include>

ui-confmgmt.xml文件源码
 
<struts>
<package name="confmgmt" extends="struts-default" namespace="/confmgmt">
        <action name="getAllUserInfoByConditions" class="userInfoAction" method="getAllUserInfoByConditions">       </action>
</package> 
</struts>


beanCntxt-confmgmt-action.xml中代码
	<bean id="userInfoAction" class="com.uy.userinfo.UserInfoAction"
scope="prototype">
<property name="userInfoService" ref="userInfoService" />
</bean>

beanCntxt-confmgmt-service.xml中代码
	<bean id="userInfoService"
class="com.uy.service.userinfo.impl.UserInfoServiceImpl"
parent="baseService">
</bean>

beanCntxt-confmgmt.xml 中代码
<import resource="beanCntxt-confmgmt-service.xml" />
<import resource="beanCntxt-confmgmt-communication.xml" />
<import resource="beanCntxt-confmgmt-action.xml" />


请问为什么我这么配置,运行的时候根本找不到这个userInfoAction这个类,还需要配置别的地方么?
另外如果在ui-confmgmt.xml中制定UserInfoAction,就可以。

<action name="getAllUserInfoByConditions" class="com.uy.action.userinfo.UserInfoAction" method="getAllUserInfoByConditions"></action>


因为刚刚接触,所以很多地方不是很懂。
------解决思路----------------------
现在使用注解不是很方便吗 ?而且感觉你的配置文件好像是挺乱的.....
------解决思路----------------------
注解和配置文件都可以啊,同一个工程里面也可以采用两种方式结合的方式来管理bean
在struts.xml中配置action的请求,配置bean名称或id,然后在applicationContext.xml中去配置bean
容器启动的时候,spring会去帮你把bean的原型加载到内存中,然后根据需要,由spring给你new bean出来

------解决思路----------------------
首先看看你的路径是否正确,配置文件是否在同一个路径下。类包与配置文件是否在同一路径下。
再看你那个action是否有默认的无参构造方法,因为spring实例化bean会默认调用无参构造方法
如果你的action有自己定义的构造方法,需要在spring中配置具体构造方法。
楼主可以试试在新建一个简单测试类,看看是否可以实例化,如果也不行,有可能是你配置文件错误。
看看启动是否报错。
------解决思路----------------------
dao\你配置了吗?

 引用的是数据源、你都没有配置、绝对报错、、
------解决思路----------------------
根据楼主情况,有很大可能是没导入struts2-spring-plugin-2.3.16.3.jar包,
导致struts2实例化UserInfoAction时,是struts2按照xml文件里<action>配置的属性自己去实例 化UserInfoAction,而不是去向spring容器要UserInfoAction实例。
所以才会出现楼主最后一段描述的情况。
  相关解决方案