ssh中整合,为什么将Action交由Spring托管,才可以在Action中使用依赖注入!

ssh中整合,为什么将Action交由Spring托管,才可以在Action中使用依赖注入!

传统正确的整合方式:

struts-config.xml:

<struts-config>

  <data-sources>

  <form-beans>

    <form-bean name="loginForm" type="com.yourcompany.struts.form.LoginForm">

  </form-beans>

  <action-mappings>

    <action <br="">      attribute="loginForm"

      input="/form/login.jsp"

      name="loginForm"

      path="/login"

      scope="request"

      type="org.springframework.web.struts.DelegatingActionProxy">  //使用代理

      <forward name="show" path="/show.jsp">

    </action>

  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.ApplicationResources">

<plug-in classname="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml">

</plug-in>

</struts-config>



applicationContext.xml:

<beans>

<bean id="personDAO" class="com.deng.PersonDAO">



<bean name="/login" class="com.yourcompany.struts.action.LogAction">

<property name="personDAO" ref="personDAO">

</property>

</bean>

</beans>



web.xml:



<context-param>   

<param-name>contextConfigLocation</param-name>   



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

</context-param>   

<listener>   

<listener-class>org.springframwork.web.content.ContextLoaderListener</listener-class>   

</listener> 





这样一来:我想在action里面注入server,只要在action里面

privaet PersonDAO personDAO;

public PersonDAO getPersonDAO() {

return personDAO;

}

public void setPersonDAO(PersonDAO personDAO) {

this.personDAO = personDAO;

}



就可以拿到personDAO对象想干吗就干吗了。。





我的问题是:



我前几天一个不小心有个问题没弄明白:为什么将Action交由Spring托管,才可以在Action中使用依赖注入!

我的:

struts-config.xml:

<action-mappings>

    <action <br="">      attribute="loginForm"

      input="/form/login.jsp"

      name="loginForm"

      path="/login"

      scope="request"

      type="com.yourcompany.struts.action.LogAction">  //不使用代理

      <forward name="show" path="/show.jsp">

    </action>



applicationContext.xml:

<beans>

<bean id="personDAO" class="com.deng.PersonDAO">



<bean name="/login" class="com.yourcompany.struts.action.LogAction">  //就象和<bean id="personDAO" class="com.deng.PersonDAO">一样<property name="personDAO" ref="personDAO">

</property>

</bean>

</beans>



然后在action里面

privaet PersonDAO personDAO;

..get()和..set()居然拿到的personDAO是空。为什么??



为什么:<bean id="personDAO" class="com.deng.PersonDAO">直接在spring里面直接写可以,而action非要托管才能注入,不能象server啊 dao之类的直接在applicationContext.xml里面写配置。



请大家教下我,谢谢了!

struts框架来支持产生的

不让spring侦听,spring读不到上下文,就不能动态给你生成类
肯定是全是空的

这就想你不启动tomcat,访问8080端口;肯定访问不到啊~
呵呵

你不使用代理,Spring监听不到啊

如果你没有配置 在请求Action的时候

spring 就拦截不到Action 所以无法执行依赖注入。

不给Spring托管 还要让Spring帮你注入 你不觉得很奇怪么?

因为Action 类不是普通的类似于我们自己写的 dao层 或者service层 的类,

它是由Struts 框架来支持产生的,所以说不能简单的用类似于dao层或者 service层

的依赖注入方式来,所以要使用Spring 中的 DelegatingActionProxy(授权Action代理),实现了针对实际Action的调用代理,struts最终调用的将是由

spring管理的Action实例,这样客户端发送的各种请求就可以用spring的Ioc设计思

想实现了。

详细的你可以百度一下 DelegatingActionProxy 的意思,其实本身struts框架中也包含这个的。