UserAction有个属性 private IUserDao userDao;第一种写法
在struts.xml中 <action name="userAction" class="com.my.web.UserAction"></action>在applicationContext.xml中<beans>
      <bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>这么写运行成功。但是why?这个action是由spring ,还是struts还是strut2-spring-plugin产生的?
第二种写法
在struts.xml中 <action name="userAction" class="UserAction01"></action>在applicationContext.xml中<beans>
    <bean  id="UserAction01" class="com.my.web.UserAction">
    </bean>
     <bean name="userDao" class="com.my.dao.UserDao"></bean>
</beans>为什么第二种写法会空指针异常?为什么不像第一种方法给你自动注入属性?这个action是由spring ,还是struts还是strut2-spring-plugin产生的?请各位指点下

解决方案 »

  1.   

    很长时间没弄这东西了 但可以肯定的是 spring生产action的
      

  2.   

    spring和struts整合后,都是spring去管理了,那个plug是spring里面的,struts等于说已经没它的什么事了!
      

  3.   

    第二种写法改为:
    <beans>
        <bean  id="UserAction01" class="com.my.web.UserAction">
            <property name="userDao" ref="userDao"></property>
        </bean>
         <bean name="userDao" class="com.my.dao.UserDao"></bean>
    </beans>
    就不会报空指针了吧
      

  4.   

    第一种没有注入,<action name="userAction" class="com.my.web.UserAction"></action>这样写还是由struts自己创建的,只有class里面的改成spring容器声明的bean才算是注入了
      

  5.   

    就你目前贴的这么多,我认为2种都不是spring生产的action
      

  6.   

    同意五楼的,第一种是struts产生的action,要想让spring产生action,就必须把本该由struts中的class处理过程交给bean中的class处理,简便的方法是在XXAction上用注解(annotation)@component("xx"),将xx"设置为和struts中的class相同即可。这样子就把处理过程交给spring了
      

  7.   

    structs 和 spring 结合有两种方式 (我用过的)1 。 就是 structs的Action 继承ActionSuppot 然后来getBean2. Action 继承自己的抽象Action   但让spring来管理Action 对象你的第一种写法 不算给spring 管理吧~~第二种 写Action的时候 吧IUserDao userDao;当Action的属性注入  也就算好了
      

  8.   

    第一种没有用到 spring ..是struts2 自己管理的,为什么他会自动注入?因为webwork(struts2 的前身) 他自身也有一套 ioc 的机制,所以他帮你注入了事实上webwork和spring结合是有点尴尬的,他们有很多重复的地方。。在其合并到 struts2后,可以指定ioc通过spring来完成,主要是通过struts2的spring插件来完成的
      

  9.   

    Spring的自动注入有两种方式,一种是ByName,一种是ByType,ByName的话,要求命名规则符合其要求,比如
    UserAction中有设置属性的方法叫setUserDao,则你的Bean的名字应叫userDao。
      

  10.   

    补充一下,默认情况下,struts-spring-plugin里面似乎指定的是ByName
      

  11.   

    昨天读了下文档,原来文档上已经写的很清楚了,在struts2的Spring Plugin 文档里:
    in struts.xml you specify the class for each Action. When using the default SpringObjectFactory, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.也就是说第一种action还是spring 产生的,是struts让spring自动注入的   you might want the bean to be completely managed by Spring.如果要完全的将action交给spring管理,那么all you have to do is configure the bean in your Spring applicationContext.xml and then change the class attribute from your Action in the struts.xml to use the bean name defined in Spring instead of the class name.
    第二种是action已经完全交给了Spring管理,所以不配置不会自动注入不知道理解的对不对。
      

  12.   


    显然你还是没理解 struts2.。  你看的是struts2的spring插件的文档。很显然你的第一种方式,即使你不用spring,也能运行正常。 和spring又有何关系呢?
    不与spring集成时,默认的是 org.apache.struts2.impl.StrutsObjectFactory, 
    可省略<constant name="struts.objectFactory" />的配置项。
    与spring集成时是  com.opensymphony.xwork2.spring.SpringObjectFactory
    需要在配置文件中指定<constant name="struts.objectFactory" value="spring" />