整合spring和hibernate的三种方式,小结之.

 

1) 在struts中使用webapplicationcontext调用spring
    声明web.xml,声明一个contextloadlistener,让在启动时执行该listener,读spring的配置文件

    <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

  再增加一个contextConfigLocation
   <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>

 WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

  UserBean userbean=(UserBean)ctx.getBean("userbean");

 

2) 将struts的action托管给spring
       这也是很经常用的.用法是
    在struts-config.xml中,加载contextloaderplugin插件,加载spring配置

 

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />


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

  这样的话,把struts的action完全托管给spring了,在struts-config.xml中

    <action path="/user/"..>,这里不需要再用class了,

但在application-context.xml中,则要有
   <bean name="/user"..../>了.

 

3 继承spring的actionsupport类
      比如

   public class aaaa extends DispatchActionSupport

 {

        ......

 

        WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

  UserBean userbean=(UserBean)ctx.getBean("userbean");

 

 

 

   }
   

 


   

相关文章:

  • 2022-12-23
  • 2021-07-20
  • 2021-04-06
  • 2021-09-16
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-06-09
  • 2021-09-11
  • 2022-12-23
相关资源
相似解决方案