Spring基础(十二):S2SH整合

1.将Spring与Hibernate整合


<bean id="sessionFactory" class="org.framework.orm.hibernate2.LocalSessionFactoryBean">
    <property name="configLocation>
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>
<bean id="sessionFactory1" class="org.framework.orm.hibernate2.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list><value>hibernate/Classes.hbm.xml</value></list>
    </property>
    <property name="hibernateProperties">
        <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
    </property>
</bean>
 
 
<bean id="classesDao" class="hibernate.ClassesDaoImpl">
    <property name="sessionFactory">
        <ref bean="sessionFactory1"/>
    </property>
</bean>

2.struts.xml

<struts>
    <constant name="struts.devMode" value="true"/>  //修改配置文件不用重启
    //异常处理
    <package name="struts-global" namespace="/" extends="struts-default">
        <global-results>
            <result name="errHandler" type="chain">
                <param name="actionName">errorProcessor</param>
            </result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="errHandler"/>
        </global-exception-mappings>
        <action name="errorProcessor" class="exception.ErrorProcessor">
            <result>error.jsp</result>
        </action>
    </package>
    <include file="struts2/struts-classes.xml"></include>
</struts>

3.struts-classes.xml

<struts>
    <package name="classes" namespace="/" extends="struts-global">
        <action name="classesAction_*" method="{1}" class="classesAction">
            <result>class.jsp</result>
        </action>
    </package>
</struts>

4.web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

5.原理

           (1).当tomcat启动时,ContextLoaderListener调contextInitialized(){this.contextLoader=createContextLoader();}

                      会按照如下规则加载配置文件:

                      <1>.若web.xml有contextConfigLocation参数,会按照指定路径加载配置文件

                      <2>.若无<context-param>,会加载WEB-INF/applicationContext.xml

                      <3>.在WEB-INF下路径可有如下写法:WEB-INF/*.../application-*.xml

                                 (注:WEB-INF下配置文件无法测试)

           (2).this.contextLoader.initWebApplicationContext(event.getServletContext());加载配置文件,启动spring容器

                      <1>.当启动web容器时,dao层,service层方法全部实例化

                      <2>.因为有aop机制,所有目标类全部创建代理对象

                      <3>.在service层和dao层,调用所有set方法或构造器进行注入

                      <4>.action层并没有创建对象,因为是多例

           (3).<struts/>里的class做了什么?

                      <1>.加载4个配置文件:default.properties,struts-default.xml,struts.plugin.xml,struts.xml

                      <2>.其中struts-plugin.xml多加载了一个xml文件:

                                 <bean type="xxx.ObjectFactory" name="spring" class="xxx.StrutsSpringObjectFactory"/>

                                 <constant name="struts.objectFactory" value="spring"/>

                                 注:该类改变action创建方式

                                            先从spring中查找action,根据Struts2配置文件中action的class属性与Spring中ID做匹配

           (4).当请求一个url并去查找一个action时,去spring查找,找到后:

                      <1>.创建action

                      <2>.在action中注入service(代理)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹏哥哥啊Aaaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值