三大框架学习心得

来源:互联网 发布:淘宝小石头运动 证吗 编辑:程序博客网 时间:2024/05/06 01:57

三大框架都用到了java 的反射机制。在Struts中,Action代理正是利用了Java的反射机制创建出了Action实例;在Hibernate中,在将数据库中的数据封装成Java对象时,需要调用pojo的set方法,这用的就是反射机制;在Spring中,它管理的bean都是通过反射机制创建出来的


hibernate中的延迟加载

  1. 延迟加载的实现的原理是cglib动态字节码  
  2.         Hibernate对延迟加载的实现原理是CGLIB动态字节码生成技术,即返回的实体并非真正的实体对象,而是经过CGLIB处理后的代理实体,当调用某一未经加载的属性时,代理实体就可以截获这一调用,然后由Hibernate实现动态加载。  
  3.           
  4.         如果要使用Hibernate的延迟加载特性,则渲染视图阶段不能关闭事务,因此,事务的范围变为整个HTTP请求的周期。  
  5.           
  6.         采用OpenSessionInView模式可以将事务范围界定在请求开始和渲染视图结束后,使得Hibernate的Session在视图渲染时仍有效。有两种方式实现OpenSessionInView模式,一种是使用Spring提供的OpenSessionInViewInterceptor,如果采用Spring MVC框架,可以将这个Interceptor加入到Controller的拦截器链中,事务在Controller处理前开始,在视图渲染后结束,如图11-17所示。  
  7.           
  8.           
  9.           
  10.         如果Web层没有采用Spring的MVC框架,而是使用Struts等其他MVC框架,甚至没有使用MVC框架,此时,就无法定义Interceptor,只能采用Filter来实现OpenSessionInView模式。  
  11.           
  12.         OpenSessionInViewFilter是Spring提供的一个Filter。在OpenSessionInViewFilter模式下,所有的HTTP请求都将被OpenSessionInViewFilter截获,事务在请求处理前开始,在请求处理完毕后结束,而不管采用何种MVC框架,甚至直接使用JSP,如图11-18所示。  
  13.           
  14.         org.springframework.orm.hibernate3.support.OpenSessionInViewFilter(延迟加载,)  
  15.           
  16.         图11-18  
  17.           
  18.         两种方式各有优劣。OpenSessionInViewInterceptor只能用于Spring MVC,但是配置简单,无须过滤URL;OpenSessionInViewFilter适用范围更广,但是必须手动配置web.xml文件,并且必须正确过滤URL。  
  19.           
  20.         无论如何,采用以上两种方式的目的都是为了使用Hibernate的延迟加载特性。由于事务也是一种数据库资源,事务持续的时间越久,数据库资源被锁定也越久,应用程序的吞吐量就会降低。因此,要尽量将事务限定在最小的范围内  



在Struts中,约定大于配置。前台请求的路径如project!list.action ,按照Struts的约定,则会跳到ProjectAction中执行list方法 ,

                                                      如果该方法返回的字符串是info,那么按照Struts的约定 所以 web包中放的Action类取名都是xxxAction(此处为ProjectAction),而放在WEB-INF下的content文件夹下的JSP取名都是xxx-xxx.jsp(此处为project-info.jsp)。

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><constant name="struts.action.extension" value="jspx"></constant><constant name="struts.convention.default.parent.package" value="myPackage" /> <constant name="struts.convention.package.locators" value="web"/> <constant name="struts.convention.package.locators.basePackage" value="com" /> 这两行明确了Action类存放的位置<package name="myPackage" extends="struts-default"><!--约定大于配置,所以这里省略了Action的配置--></package></struts>


Spring中最核心的组建就是Bean,原来Spring解决了一个非常关键的问题他可以让 你把对象之间的依赖关系转而用配置文件来管理,也就是他的依赖注入机制

Context就是一个Bean关系的集合,这个关系集合又叫Ioc容器,一旦建立起这个Ioc容器后Spring就可以为你工作了

Core就是发现、建立和维护每 个Bean之间的关系所需要的一些列的工具,从这个角度看来,Core这个组件叫Util更能让你理解。



在运用Spring框架将Struts、Hibernate整合时,与单独使用它们相比,配置文件发生了那些变化呢?


ssh框架整合前后配置文件的变化单独使用整合使用单独使用Struts时,需要①在web.xml中配置Struts2的过滤器
②在源文件夹下建立Struts的配置文件,内容主要为对Action的配置①仍需要在web.xml中配置Struts2的过滤器
②仍在源文件夹下建立Struts的配置文件,区别在于根据
约定大于配置,省略了对Action的配置,增加了三个常量
说明Action组建的位置
单独使用Hibernate时,需要①在实体包中建立数据映射文件xxx.hbm.xml,
内容主要为对Java对象和数据库表中的数据之间映射的配置
②在源文件夹下建立Hibernate.cfg.xml,内容主要为对使用的数据库的配置
以及对数据映射文件位置的配置
③在源文件夹下建立ehcache.xml文件①可以仍在实体包中建立数据映射文件,但经常采用JPA注释
取代基于xml的数据映射的配置

省略了Hibernate.cfg.xml,对数据库的配置在源文件夹下的
applicationContext.xml中进行
③仍在在源文件夹下建立ehcache.xml文件单独使用Spring,需要
①在源文件夹下建立applicationContext.xml的配置文件
主要内容为对Bean节点的配置以及对Bean之间关系的说明

①仍在在源文件夹下建立applicationContext.xml的配置文件

②需要在web.xml中进行一系列的配置

基于注释的ssh项目中的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:p="http://www.springframework.org/schema/p"       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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 对目标包使用注释注入 --><context:component-scan base-package="com"/> 后者没有这一行<!-- 声明注释的驱动 --><tx:annotation-driven transaction-manager="transactionManager"/> <!-- 声明dataSource(使用dbcp,也可以使用c3p0),运用了数据库连接池技术,--><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///projectdb"></property><property name="username" value="root"></property><property name="password" value="root"></property><property name="initialSize" value="5"></property><property name="maxActive" value="20"></property></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="packagesToScan" value="com.pojo"></property><property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop> </props></property></bean><!-- 声明JDBC事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 声明 HibernateTemplate--><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property></bean></beans>


基于xml文件配置的ssh项目中的applicationContext.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.   
  7.     <bean id="dataSource"  
  8.         class="org.apache.commons.dbcp.BasicDataSource">  
  9.         <property name="driverClassName"  
  10.             value="com.mysql.jdbc.Driver">  
  11.         </property>  
  12.         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
  13.         <property name="username" value="root"></property>  
  14.         <property name="password" value="1234"></property>  
  15.     </bean>  
  16.   
  17.     <bean id="sessionFactory"  
  18.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  19.         <property name="dataSource">  
  20.             <ref bean="dataSource" />  
  21.         </property>  
  22.         <property name="hibernateProperties">  
  23.             <props>  
  24.                 <prop key="hibernate.dialect">  
  25.                     org.hibernate.dialect.MySQLDialect  
  26.                 </prop>  
  27.                 <prop key="hibernate.show_sql">true</prop>  
  28.             </props>  
  29.         </property>  
  30.         <property name="mappingResources">  
  31.             <list>  
  32.                 <value>com/coderdream/Account.hbm.xml</value>  这几行旨在说明数据映射文件的位置,与前者对pojo包的扫描相对应
  33.             </list>  
  34.         </property>  
  35.     </bean>  
  36.   
  37.     <!-- 引用Hibernate的事务管理器 -->  
  38.     <bean id="transactionManager"  
  39.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  40.         <property name="sessionFactory" ref="sessionFactory"></property>  
  41.     </bean>  
  42.   
  43.     <bean id="accountDAO" class="com.coderdream.AccountDAO">  
  44.         <property name="sessionFactory" ref="sessionFactory"></property>  
  45.     </bean>  
  46.   
  47.     <!-- 通过事务管理器来管理Service -->  
  48.     <bean id="accountService"  
  49.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  50.         <property name="transactionManager" ref="transactionManager"></property>  
  51.         <property name="target">  
  52.             <bean class="com.coderdream.AccountService">  
  53.                 <property name="accountDAO" ref="accountDAO"></property>  
  54.             </bean>  
  55.         </property>  
  56.         <property name="transactionAttributes">  
  57.             <props>  
  58.                 <prop key="transfer">PROPAGATION_REQUIRED</prop>  
  59.             </props>  
  60.         </property>  
  61.     </bean>  
  62. </beans> 


ssh项目的配置文件情况 位置主要配置项web.xmlWebRoot/WEB-INF/

①Spring的字符集过滤器---统一网页的码型

②openSessionInview-----为了将Hibernate的Session生命周期延长至一次Http请求,满足对视图的渲染

③Struts过滤器----Struts的核心控制器

④Spring的监听器-----为了读取到ApplicationContext.xml文件

applicationContext.xml源文件夹etc下①启用注释进行依赖注入,使用注释定义事务
②配置数据源
③配置Hibernate会话工厂sessionFactory
④事务管理器transactionManager
applicationContext-shiro.xml源文件夹etc下 struts.xml源文件夹etc下主要对Action进行配置
如果遵循了“约定”,无需对Action进行配置,但需要增加常量说明Action的包位置ehcache.xml源文件夹etc下<?xml version="1.0" encoding="UTF-8"?>
<ehcache> 
    <diskStore path="java.io.tmpdir"/> 
    <defaultCache 
        maxElementsInMemory="10000" 
        eternal="false"
        timeToIdleSeconds="120" 
        timeToLiveSeconds="120" 
        overflowToDisk="true"
        /> 
</ehcache>Struts.xml的配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 字符集过滤器 --><filter><filter-name>springEncoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>springEncoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- OpenSessionInview 作用??延长session的生命周期--><filter>   <filter-name>openSessionInView</filter-name>   <filter-class>   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   </filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- shiroFilter 权限框架 (session 与 struts 之间)<filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>--><!-- Struts框架,本来Struts就需要在web.xml中配置一个过滤器,现在不变 --><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><!-- 监听器,为了读取到applicationContext文件--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>   classpath:applicationContext*.xml  </param-value></context-param>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>