小七 发表于 2014-5-27 22:39

SSH2完整整合项目流程(巨详细)

1、创建Web Project
2、web工程与struts2整合
   (1)添加struts2类库
      struts2-core-2.1.8.1.jar
      xwork-core-2.1.6.jar
      commons-fileupload-1.2.1.jar
      commons-io-1.3.2.jar
      ognl-2.7.3.jar
      freemarker-2.3.15.jar
    (2) 在web.xml文件中添加struts2集成
            <filter>
                <filter-name>struts</filter-name>
                <filter-class>
                        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
                </filter-class>
                <init-param>
                        <param-name>config</param-name>
                        <!--
                        修改struts2默认配置文件,
                        必须把struts-default.xml,
                        struts-plugin.xml一起添加
                        必须:按顺序添加
                        -->
                        <param-value>
                                struts-default.xml,struts-plugin.xml,config/struts/struts.xml
                        </param-value>
                </init-param>
        </filter>
        <filter-mapping>
                <filter-name>struts</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!--struts2 *.actionstruts2*.do-->
        (3)创建struts2配置文件struts.xml配置
        (4)完成页面跳转
3、测试struts2工程
4、struts2与spring集成
    (1)添加spring类库与struts支持spring插件
       struts2-spring-plugin-2.1.8.1.jar
       cglib-nodep-2.1_3.jar
       commons-logging.jar
       log4j-1.2.15.jar
       spring.jar
       dom4j-1.6.1.jar
      (2)在web.xml中完成spring集成
      <listener>
                <listener-class>
                        org.springframework.web.context.ContextLoaderListener
                </listener-class>
        </listener>
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                        classpath:config/spring/applicationContext.xml
                        <!--classpath:config/spring/applicationContext*.xml-->
                </param-value>
        </context-param>
        (3)创建spring配置文件applicationContext.xml
        (4)在struts2配置文件中添加spring 集成常量
        <!-- 设置集成spring框架 -->
        <constant name="struts.objectFactory" value="spring"/>
        (5)spring管理struts2中Action
           (1)在spring 配置文件中加入bean管理
        <bean id="userAction" class="com.tarena.web.action.UserAction" >
        </bean>
           (2)修改struts-user.xml配置
      <action class="userAction" >
                </action>
5测试struts+spring集成
6 spring+hibernate集成
   (1)添加hibernate类库
      slf4j-nop-1.5.8.jar
      slf4j-api-1.5.8.jar
      jta-1.1.jar
      javassist-3.9.0.GA.jar
      hibernate3.jar
      commons-collections-3.1.jar
      antlr-2.7.6.jar
   (2)添加MySQL驱动类库
      mysql-connector-java-5.1.16-bin.jar
   (3)创建实体类
   (4)编写User.hbm.xml映射文件
   (5)反向生成表结构
   (6)spring管理hibernate
    在spring applicationContext.xml配置文件中添加spring对hibernate管理
       (A) 添加DBCP连接池
          a、添加类库
             commons-dbcp.jar
             commons-pool.jar
          b、applicationContext.xml在配置连接池
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
               <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb"/>
               <property name="username" value="root"/>
               <property name="password" value="1234"/>
               <property name="initialSize"value="2"/>
               <property name="maxActive" value="8"/>
               <property name="maxIdle" value="5"/>
               <property name="minIdle" value="3"/>
               <property name="maxWait" value="3000"/>
               <property name="removeAbandonedTimeout" value="60000"/>
               <property name="removeAbandoned" value="true"/>
       </bean>
        连接池参数如下:
                        initialSize
                        连接池启动时创建的初始化连接数量
                        (默认值initialSize :连接池启动时
                        创建的初始化连接数量(默认值为0)
                        maxActive
                        连接池中可同时连接的最大的连接数
                        (默认值为8 ,调整为20,高峰单机器在20并发左右,
                        自己根据应用场景定)
                        maxIdle
                        连接池中最大的空闲的连接数,
                        超过的空闲连接将被释放,
                        如果设置为负数表示 不限制(默认为8个)
                        minIdle
                        连接池 中最小的空闲的连接数,低于这个数量会被
                        创建新的连接(默认为0,调整为5, 该参数越接近
                        maxIdle,性能越好,因为连接的创建和销毁,
                        都是需要消耗资源的,但是不能太大,
                        因为在机器很空闲的时候,也会创建低于
                        minidle个数的连接)
                        maxWait
                        最大等待时间,当没有可用 连接时,
                        连接池等待连接释放的最大时间,
                        超过该时间限制会抛出异常,
                        如果设 置-1表示无限等待(
                        默认为无限,调整为60000ms,
                        避免因线程池不够用,而导致 请求被无限制挂起)
                        removeAbandonedTimeout:
                        超过时间限制,回收没有用(废弃)的连接
                        (默认为 300秒,调整为180)
                        removeAbandoned:
                        超过 removeAbandonedTimeout时间后,
                        是否进行没用连接(废弃)的回收
                        (默认为 false,调整为true)
        (B)spring管理hibernate实体映射文件:
        <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.dialect">
                                        org.hibernate.dialect.MySQLDialect
                                </prop>
                                <prop key="hibernate.show_sql">true</prop>
                                <prop key="hibernate.format_sql">true</prop>
                        </props>
                </property>
                <property name="mappingLocations">
                        <list>
                                <value>
                                        classpath:config/hibernate/hbm/User.hbm.xml
                                </value>
                        </list>
                </property>
        </bean>
        (C)spring读写属性(properties)文件
                a、创建属性文件 db.properties
                b、在applicationContext.xml配置
                  <bean id="propertyPlaceholderConfigurer"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                        <list>
                                <value>classpath:config/props/db.properties</value>
                        </list>
                </property>
        </bean>
       c:引用: ${username}
               
       (8)spring分文件管理
                   <!-- 引入其他spring 配置文件 -->
       applicationContext.xml引入下列文件
        <import resource="applicationContext-web.xml"/>
        <import resource="applicationContext-biz.xml"/>
        <import resource="applicationContext-dao.xml"/>
7、开发数据访问层
    使用spring提供hibernate 模板类 HibernateDaoSupport
    UserDaoImpl extends HibernateDaoSupport
    在UserDaoImpl注入到spring 配置文件 applicationContext-dao.xml
    <bean id="userDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">
                <!--HibernateDaoSupport中的sessionFactory-->
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>
8、添加spring声明事务管理(AOP)
   声明式事务管理:
      添加spring 类库
         aspectjrt.jar
         aspectjweaver.jar
   了解事务的几种传播特性

   (1)PROPAGATION_REQUIRED:
    如果存在一个事务,则支持当前事务,如果没有事务则开启。
   (2)PROPAGATION_SUPPORTS:
   如果存在一个事务,支持当前事务。
   如果没有事务,则非事务的执行。
   (3)PROPAGATION_MANDATORY:
   如果已经存在一个事务,支持当前事务。
   如果没有一个活动的事务,则抛出异常。
   (4)PROPAGATION_REQUIRES_NEW:
   总是开启一个新的事务。如果一个事务已经存在,
   则将这个存在的事务挂起。
    (7)PROPAGATION_NESTED:(spring特有)
   如果一个活动的事务存在,则运行在一个嵌套的事务中.
   如果没有活动事务, 则按TransactionDefinition.
   PROPAGATION_REQUIRED 属性执行

   (5)PROPAGATION_NOT_SUPPORTED:
   总是非事务地执行,并挂起任何存在的事务。
   (6)PROPAGATION_NEVER:
   总是非事务地执行,如果存在一个活动事务,则抛出异常。

       
        <!-- 配置声明事务管理 spring2.x-->
        <!-- 定义事务管理 -->
        <bean id="transactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <!-- 事务规则 -->
        <tx:advice id="tmAdvice" transaction-manager="transactionManager">
                <tx:attributes>
                  <!--*通配符 save* 方法名必须是以save开始-->
                        <tx:method name="save*" propagation="REQUIRED" />
                        <tx:method name="update*" propagation="REQUIRED" />
                        <tx:method name="delete*" propagation="REQUIRED" />
                        <tx:method name="find*" read-only="true" />
                        <!-- read-only 只读:-->
                </tx:attributes>
        </tx:advice>
        <aop:config>
                <!-- point cut -->
                <aop:pointcut id="pointCut"
                        expression="execution(* com.tarena.biz.*.*(..))" />
                <aop:advisor advice-ref="tmAdvice" pointcut-ref="pointCut"/>       
        </aop:config>
       
9、测试
    添加测试类库spring2.5.6
   junit-4.4.jar
   spring-test.jar
   //使用spring测试
   @RunWith(SpringJUnit4ClassRunner.class)
   //加载spring配置文件
   @ContextConfiguration(locations = {
                "classpath:config/spring/applicationContext.xml",
                "classpath:config/spring/applicationContext-dao.xml",
                "classpath:config/spring/applicationContext-biz.xml",
                "classpath:config/spring/applicationContext-web.xml" })
   //自动注入
   @Autowired
        private IUserDao iuserDao;
       
        @Test
        public void saveUserTest(){
          //测试方法
        }
10、完成业务层代码编写
11、完成表示层代码编写
12、测试工程

13、spring处理hibernate延时加载
        1、配置延时过滤器
          OpenSessionInViewFilter.class
        2、在web.xml配置OpenSessionInViewFilter
       
                <filter>
                <filter-name>openSessionInViewFilter</filter-name>
                <filter-class>
                        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
                </filter-class>
                <init-param>
                        <param-name>sessionFactoryBeanName</param-name>
                        <param-value>sessionFactory</param-value>
                </init-param>
        </filter>
        <filter-mapping>
                <filter-name>openSessionInViewFilter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>

页: [1]
查看完整版本: SSH2完整整合项目流程(巨详细)