Cyh的博客

Email:kissyan4916@163.com
posts - 26, comments - 19, trackbacks - 0, articles - 220

Spring &Hibernate集成

Posted on 2009-02-16 20:02 啥都写点 阅读(187) 评论(0)  编辑  收藏 所属分类: J2EE

缺陷:依赖了UserManager

原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象

1springstruts依赖库配置

   * 配置struts

      --拷贝struts类库和jstl类库

      --修改web.xml文件来配置ActionServlet

      --提供struts-config.xml文件

      --提供国际化资源文件

   * 配置spring

      --拷贝spring类库

      --提供spring配置文件

2、在strutsAction中调用如下代码取得BeanFatory

BeanFactoryfactory=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession.getServletContext())

3、通过BeanFactory取得业务对象,调用业务逻辑方法

 

web.xml中做如下配置

<context-param>

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

<param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</param-value>  变色部分如果已经在CLASSPATH中配置了可省略

 </context-param>

 

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

  </listener>

servletConetext的一个助手类,可以读取它àWebApplicationContextUtils

Spring &Hibernate集成方案2



原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询。

1springstruts依赖库配置

   * 配置struts

      --拷贝struts类库和jstl类库

      --修改web.xml文件来配置ActionServlet

      --提供struts-config.xml文件

      --提供国际化资源文件

   * 配置spring

      --拷贝spring类库

      --提供spring配置文件

2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑注入过来

3、在struts-config.xml文件中配置action

* <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值到IOC容器中取得本次请求对应的Action

4、在spring配置文件中需要定义strutsAction,如:

   <bean name=”/login” class=”com.bjsxt.usermgr.actions.LoginAction” scope=”prototype”>

      <property name=”userManager” ref=”userManager”/>

   </bean>

l       必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致

l       必须注入业务逻辑对象

l       建议scope设置为prototype,这样就避免了struts Action的线程安全问题

struts中应该修改为:

 <action path=”/login”

         Type=”org.springframework.web.struts.DelegatingActionProxy”

         Name=”loginForm”

         Scope=”request”

</action>

ApplicationContext中需要做如下修改:

   不能有id属性 而是改成name属性(路径应该和struts中的配置的actionpath相同)

<bean name=”/login” class=”com.bjsxt.usermgr.action.LoginAction” scope=”prototype”>

 <property name=”userManager” ref=”userManagr”/> //变色部分代表从单实例变成多实例

</bean>



                                                                                                       --    学海无涯
        


只有注册用户登录后才能发表评论。


网站导航: