SSH三大框架整合分析

1. Springs整合struts2核心包:

spring-web-4.2.4.RELEASE.jar (下边的B监听器在这个包中)

   我们首先来标记两监听器:

A监听器:javax.servlet.ServletContextListener

这个监听器监听着ServletContext的创建和销毁 ,因此项目一旦启动,就会触发监听器的行为;

B监听器:org.springframework.web.context.ContextLoaderListener

这个监听器实现了A监听器,所以一旦服务器启动,就会触发该监听器行为,然而这个监听器的行为不是别的,就是加载Spring的配置文件,并初始化里边的单例bean;

需要特别提醒的是:

1. 这些单例bean中包括在dao层用到的SessionFactory或者它的工厂localSessionFactoryBean;

2. 甚至是web层会用到的action,都会在服务器启动的时候创建,以供struts2Spring整合,因为struts2Spring整合的本质就是actionSpring中获得service,如果actionSpring容器中,那么它从Spring中获得service岂不是易如反掌?  当然这只是其中一种整合方法;

 

struts2-spring-plugin-2.3.24.jar

1.在struts-default.xml中定义了struts2框架默认的对象工厂是ObjectFactory,这个工厂和spring没有太大关系,struts2框架中所有的action interceptor result全是bean,在struts2框架中,默认是使用struts2自己的对象工厂创建对象,

2.struts2-spring-plugin-2.3.24.jar这个jar包中有一个struts -plugin.xml文件,由于struts2的配置文件加载顺序是:

a. default.properties

b. struts-default.xml

c. struts -plugin.xml

 如果有相同属性配置,后边的配置会覆盖前面的配置,所以struts -plugin.xml

就覆盖了struts-default.xml中对默认对象工厂的配置,而是提供了一个与Spring有着千丝万缕联系的对象工厂SpringObjectFactory,这个对象工厂一方面可以通过<bean>标签中的ID值来从Spring中获取对象,也可以使用action的全类名,利用反射方式来创建action对象,并且在创建action对象的同时会扫描action类中的setter方法,根据setter方法后的属性名,到Spring容器中匹配有相同名字的bean,然后用set方法走属性注入的方式,把bean(比如service)注入到action,这样就实现了Struts2Spring的整合;

 

2.Spring整合Hibernate核心包: 

                              ( 基本原理:就是由spring来管理hibernateSessionFactory )

          spring-orm-4.2.4.RELEASE.jar

这个包是Spring提供,在这个包下有一个类:org.springframework.orm.hibernate5.LocalSessionFactoryBean ,这个类是SessionFactory工厂类的,它的内部持有对SessionFactory的引用,LocalSessionFactoryBean 这个类(是Spring提供的当然)可以被配置到Spring容器中,并且这个类提供了大量的属性,让你注入,比如说 private DataSource dataSource;用于接收注入的数据源(连接池),private Properties hibernateProperties;用户接收注入的hibernate一些属性,例如方言之类,private Resource[] mappingDirectoryLocations;用于接收注入的对象映射文件,当然最终就是你把这些该注入的给设置好,LocalSessionFactoryBean 就可以给你创建SessionFactory对象了,这样就完成了SpringSessionFactory的管理;

当然这个时候,这个SessionFactory注入到dao(因为这个dao extends HibernateDaoSupport 所以实际是将SessionFactory注入到HibernateDaoSupport ),这样就可以在dao中轻松获得this.getHibernateTemplate() ,进行CRUD操作;

Ps:在后来的事务操作中,因为事务要用到session,所以在org.springframework.orm.hibernate5.HibernateTransactionManager的配置中也要传入SessionFactory ,实际传入的是LocalSessionFactoryBean ,但是LocalSessionFactoryBean 会自动调用方法

        public SessionFactory getObject() {

            return this.sessionFactory;

        }

返回sessionFactory,完成注入;


==============================以下三大框架整合web.xml配置=================================

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring_day05</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<!-- spring的配置以下 -->
<context-param>
<!-- 告诉下边的过滤器spring的配置文件的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!-- 这个监听器在web服务器启动时,给我们创建一个 WebApplicationContext对象,然后放在 ServletContext域中, 
servletContext.setAttribute (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, 
this.context); 其中: this.context = createWebApplicationContext(servletContext); -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring的配置以上 -->


<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2的过滤器配置以下 -->
<filter>
<filter-name>struts2Filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2的过滤器配置以上 -->


</web-app>

=========================以上三大框架整合web.xml配置==============================




===================以下三大框架整合applicationContext.xml配置======================

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描 @Controller @Service @Repository 才能生效 -->
<context:component-scan base-package="com.itheima" />


<!-- 加载 db.properties 文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> 
</bean>


<!-- 声明sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<value>
hibernate.show.sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
</value>
</property>
<!-- 加载注解类 扫描实体类的注解 @Entity @Table 等 -->
<property name="packagesToScan">
<list>
<value>com.itheima.domain</value>
</list>
</property>
</bean>


<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
==================以上三大框架整合applicationContext.xml配置=======================












猜你喜欢

转载自blog.csdn.net/weixin_36898943/article/details/80740527