网上商城SSH各个类间的关系(二)

来源:互联网 发布:ubuntu安装tomcat 编辑:程序博客网 时间:2024/05/17 14:20

       接着昨日的总结,昨天说到action、service、dao、vo之间的调用,以及在Struts中的配置,建立与Spring的关系。        期待Spring是如何与action建立起联系、是如何通过Spring实现解耦和?

       首先在Spring的配置文件ApplicationContext.xml中配置Dao设置,与SessionFactory关联

<!--Dao的配置=========================  --><bean id="userDao" class="cn.itcast.shop.user.dao.UserDao"><property name="sessionFactory" ref="sessionFactory"></property>   <!-- 获得Hibernate模板 --> </bean>

           然后进行Service配置:

<!--Service的配置=========================  --><bean id="userService" class="cn.itcast.shop.user.service.UserService"><property name="userDao" ref="userDao"/></bean>
      bean中的id是自己设置唯一标识,class是UserService这个实体类的全路径;property中属性name也是在此起的,ref顾名思义有链接的意思,Service是如何与Dao联系起来的呢?通过ref,ref的值与Dao配置中bean的id值相同:


        最后进行Action的配置:

<!-- 用户模块的Action --><bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype"><!-- 注入Service --><property name="userService" ref="userService"/></bean>
      代码的含义与Service的相同,与Service的对应关系也如上图所示。我们知道action、service、dao和vo是通过spring来管理的,程序启动开始执行action,通过配置文件找到对应的action,所以在action注入Service时,属性值、get和set对象的值要与配置文件中bean配置action中name值相同。如图:


      两次的总结,对他们之间的关系有初步的了解还需要进一步的深入学习。

0 0