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(代理)