spring(四) 手动整合web项目(SSH)


      清楚了spring的IOC 和 AOP,最后一篇就来整合SSH框架把,记录下来,以后应该会用的到。

                    --WZY

一、web项目中如何使用spring?

      当tomcat启动时,就应该加载spring的配置文件,而不是每次都要手动使用new  ClassPathXmlApplicationContext来加载xml。所以,spring提供了一个ContextLoaderListener。有了它,tomcat启动时,就会加载配置文件。

      导入spring.web....jar

      1、web.xml中添加监听器

1 
2     
3         class>org.springframework.web.context.ContextLoaderListenerclass>
4     

        这样配置的监听器,默认加载的是WEB-INF目录下的xml文件,而我们的配置文件在src下,所以需要进行下一步的配置。

      2、web.xml中配置 applicationContext.xml 位置

1 
6     
7         contextConfigLocation
8         classpath:applicationContext.xml
9     

      3、从ServletContext中获得spring容器,创建的容器已经放在ServletContext中了

1 //方式1:手动从ServletContext作用域获得内容
2         //WebApplicationContext applicationContext = (WebApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
3         
4         //方式2:提供工具类
5         WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

二、SSH整合所用到的jar包

      2.1、struts2所需jar

        版本:2.3.15.1

        位置:struts-2.3.15.1\apps\struts2-blank\WEB-INF\lib

          总共:13个jar包        

      2.2、hibernate

          版本:3.6.10

          所需jar

              1、核心jar:hibernate3.jar

              2、必须:lib/required 所有

              3、jpa :lib\jpa 所有

              4、c3p0 \lib\optional\c3p0

              5、 log4j 整合jar  slf4j-log4j12-1.7.2.jar

              6、mysql驱动包  mysql-connector-java-5.1.22-bin.jar

                   

              7、二级缓存  ehcache-1.5.0.jar  、backport-util-concurrent.jar 原本要导3个,logging包在前面struts包中有了,所以无需在导入

           hibernate总共:13个jar包

      2.3、spring

          版本:3.2.0

          jar包:

             核心:4个(core、beans、expression、context) + commons-logging..jar(这个jar包在前面很多地方导入过,struts2、hibernate的二级缓存、spring都需要,只需要导一份即可)

              aop:aop联盟、spring aop、aspectj、spring-aspect

                  aop联盟:spring-framework-3.0.2.RELEASE-dependencies\org.aopalliance\com.springsource.org.aopalliance\1.0.0

                  spring aop和spring-aspect:在核心包中

                  aspectj:spring-framework-3.0.2.RELEASE-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.RELEASE     

              jdbc:2个 spring-jdbc、spring-tx

              web:1个 spring-web

              test:1个 spring-test (整合测试)

          总共:12个

      2.4、整合jar

          spring3 整合hibernate3,spring提供整合jar : spring-orm...jar

          struts2 整合 spring3 ,struts提供整合jar:struts2-spring-plugin-2.3.15.1.jar

              位置:struts-2.3.15.1\lib

          总共:2个

      2.5、整理jar

          前面拿到总共39个,去除一个重复的,剩下总共38个。

三、配置文件

      3.1、hibernate

          hibernate.cfg.xml  核心配置文件

          *.hbm.xml  映射文件(domain)

      3.2、spring

          applicationContext.xml

          jdbcInfo.properties

          web.xml(ContextLoaderListener)

      3.3、struts2

          struts.xml

          web.xml(前端过滤器  StrutsPrepareAndExecuteFilter)

四、spring整合hibernate

       4.1、首先要了解hibernate的配置文件,和hibernate需要什么,然后如何将其让spring来管理。

          hibernate.cfg.xml

        通过这两个图,可以知道,spring需要帮hibernate获取session,而获取session则先要获得sessionFactory,获取sessionFactory则需要加载配置文件。所以spring就需要提供两个东西,一个sessionFactory,一个session。spring如何做的呢?

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="
 5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7 
 8 class="com.mchange.v2.c3p0.ComboPooledDataSource">
 9     
10     
11     
12     
13 
14 
15 
16 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
17     
18     
19     
20         
21             
22                 org.hibernate.dialect.MySQLDialect
23             
24             true
25             true
26             update            
27         
28     
29     
30     
31 
32 
33 
36  class="com.wuhao.dao.UserDao">
37      
38  
39 
40 
41 

        使用:

        这样一来,就可以将hibernte.cfg.xml给删除掉了。

        还有一个问题,事务的处理。

        简单,之前一篇文章我们就讲解了spring如何进行事务管理的,那时候使用的是jdbc管理器,现在换一个即可,使用hibernate事务管理器

        所以整合完hibernate后,spring的配置文件就成了这样

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                               http://www.springframework.org/schema/beans/spring-beans.xsd
 8                               http://www.springframework.org/schema/aop 
 9                               http://www.springframework.org/schema/aop/spring-aop.xsd
10                               http://www.springframework.org/schema/tx 
11                               http://www.springframework.org/schema/tx/spring-tx.xsd">
12 
13 
14 class="com.mchange.v2.c3p0.ComboPooledDataSource">
15     
16     
17     
18     
19 
20 
21 
22 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
23     
24     
25     
26         
27             
28                 org.hibernate.dialect.MySQLDialect
29             
30             true
31             true
32             update            
33         
34     
35     
36     
37 
38 
39 
42  class="com.wuhao.dao.UserDao">
43      
44  
45  
46  
47 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
48     
49 
50 
51 
52     
53         
54     
55 
56  
57 
58 class="true">
59 
60     
61 
62 

      总结:其实非常简单,一开始整合肯定毫无头绪,不知道从哪里下手,那么就先将一个最简单的hibernate操作对象的小demo写出来,看看hibernate需要什么,然后spring就配置什么,给什么。这样就简单明了了。

五、struts2整合spring

      5.1、struts2自己创建action。而不用spring帮我们创建

          struts.xml

          action中userService对象的创建就需要让spring帮我们创建

          applicationContext.xml

          这样,就可以了。

      5.2、spring帮我们创建action,struts2什么也不用做了。‘

            前提:需要导入struts-spring-plugin...jar

      全部整合完后的applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                               http://www.springframework.org/schema/beans/spring-beans.xsd
 8                               http://www.springframework.org/schema/aop 
 9                               http://www.springframework.org/schema/aop/spring-aop.xsd
10                               http://www.springframework.org/schema/tx 
11                               http://www.springframework.org/schema/tx/spring-tx.xsd">
12 
13 
17 class="com.wuhao.service.UserService">
18 
19     
20           
21 
22 class="com.wuhao.action.UserAction">
23     
24  
25                               
26 
27 
28 class="com.mchange.v2.c3p0.ComboPooledDataSource">
29     
30     
31     
32     
33 
34 
35 
36 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
37     
38     
39     
40         
41             
42                 org.hibernate.dialect.MySQLDialect
43             
44             true
45             true
46             update            
47         
48     
49     
50     
51 
52 
53 
56  class="com.wuhao.dao.UserDao">
57      
58  
59  
60  
61 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
62     
63 
64 
65 
66     
67         
68     
69 
70  
71 
72 class="true">
73 
74     
75 
76 

六、总结

       那么到这里,手动整合ssh就结束了,其实非常简单,一步步来,从导包到整合hibernate在整合struts的步骤进行,那么就可以成功整合了,上面所有的我都没有采用接口编程,为了方便,日后注意一下这点即可。非常简单。

      但是在整合hibernate时,我们的你domain都映射文件都是自己手动编写的,实际开发中肯定不需要一个个手动编写,因为是先有数据库,然后在来写你domain,所以之后会说到通过反转引擎根据数据库中的表来创建domain和映射文件。