Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1566216
  • 博文数量: 1481
  • 博客积分: 26784
  • 博客等级: 上将
  • 技术积分: 17045
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-12 09:22
文章分类

全部博文(1481)

文章存档

2014年(10)

2013年(353)

2012年(700)

2011年(418)

分类: LINUX

2013-03-12 11:02:29

Struts2 , Hibernate , Spring 都已经讲完,3个框架各司其职,给了我们很大的方便;框架也算是一种软件,只是更加的底层,我们是在别人的基础上再次的开发新的软件。

此刻对ssh的理解:Hibernate ,搬运工,进行数据的存取;Struts2,转运工,解释数据的来龙去脉;Spring , 监工,hold全场;而我们自己写的Action是操作工,对数据进行需要的处理

此处记录SSH整合的基本配置,对于Spring的配置有两种:一种是基于注解的,我们不在xml文件中配置我们写的java类的bean属性,而是在 java类中通过注解来进行标示;另一种是基于xml文件的,我们在spring的配置文件中,手动的配置bean属性,确定bean的id,class 和param。相比较而言,基于xml的配置维护起来更为方便明了



一:基于注解    

1). 加入 Spring
    > 加入 jar 包
org.springframework.aop-3.1.0.M1.jar:spring的面向切面编程,提供aop实现
.asm-3.1.0.M1.jar:spring独立的asm程序,相比2.5版本,需要额外的asm.jar包
.aspects-3.1.0.M1.jar:spring提供AspectJ框架的整合
.beans-3.1.0.M1.jar:spring IOC的基础实现
.context-3.1.0.M1.jar:spring提供在基础IOC功能上的扩展服务。提供企业级服务的支持
.context.support-3.1.0.M1.jar:spring-context的扩展支持,用于mvc方面
.core-3.1.0.M1.jar:spring3.1的核心工具包
.expression-3.1.0.M1.jar:spring的表达式语言
.instrument.tomcat-3.1.0.M1.jar:spring3.1对Tomcat的连接池的集成
.instrument-3.1.0.M1.jar:spring3.1对服务器的代理接口
.jdbc-3.1.0.M1.jar:spring对jdbc的简单封装
.jms-3.1.0.M1.jar:spring为简化JMS API 使用而做的简单封装
.orm-3.1.0.M1.jar:spring整合第三方的ORM映射支持,如Hibernate,Ibatis
.oxm-3.1.0.M1.jar:spring对Object/XML的映射的支持,可以让java与xml来回切换
.test-3.1.0.M1.jar:spring对Junit的测试框架的简单封装
.transaction-3.1.0.M1.jar:为jdbc,hibernate,jdo,jpa等提供的一致的声明式和编程式事务管理
.web.portlet-3.1.0.M1.jar:spring mvc的增强
.web.servlet-3.1.0.M1.jar:对j2ee6.0 Servlet3.0的支持
.web.struts-3.1.0.M1.jar:整合struts的支持
.web-3.1.0.M1.jar:spring web下的工具包

其中红色的一般是必须需要的,以.开头的前面是org.springframework
为了方便我们也把相关的jar包添加进来
commons-logging-1.1.1.jar:spring的事务依赖此包,日志
c3p0-0.9.1.2.jar:数据源
mysql-connector-java-5.1.7-bin.jar:mysql的jar包
com.springsource.org.aopalliance-1.0.0.jar:aop实现的辅助
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar:aop实现的辅助

    > 加入 Spring 的配置文件
applicationContext.xml
> 配置 Spring IOC 容器自动扫描的包(基于注解才需要)
在applicationContext.xml 文件中配置
    

    > 配置 web.xml 文件.
在 Web 应用的 web.xml 文件中声明一个 ContextLoaderListener 并且在同一文件里增加一个 contextConfigLocation , 这个声明决定了哪些 Spring XML 配置文件将要被加载。
    
    
        contextConfigLocation
        classpath:applicationContext*.xml
    

    
    
    org.springframework.web.context.ContextLoaderListener
    

    若没指定 contextConfigLocation 的context参数, ContextLoaderListener 将会寻找一个名为 /WEB-INF/applicationContext.xml 的文件以加载。 一旦context文件被加载,Spring 通过文件中 bean 的定义创建一个 WebApplicationContext 对象并且将它储存在 Web 应用的 ServletContext 中。
所有 Java Web 框架都构建在 Servlet API 之上,所以可以使用下面的代码片断访问这个 由 ContextLoaderListener 创建的ApplicationContext。
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
2). 在 Spring 中配置 dataSource,资源化文件
在类路径下添加jdbc.properties文件,连接数据库所需的信息
user=root
password=198921
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///jin

    > 加入外置化配置的 properties 文件
在applicationContext.xml 文件中配置



配置dataSource

        
        
        
        
    

3). 加入 Hibernate
    > 加入 jar 包
> 加入 Hibernate 的 XXX.hbm.xml 文件.
    > 加入 Hibernate 的 hibernate.cfg.xml 文件.
    在类路径中加入hibernate.cfg.xml 文件

    
    
        
        200
        
        50
        true
        true
        org.hibernate.dialect.MySQLInnoDBDialect
        
        update
        
        
    



4). Spring 整合 Hibernate.    
    > 在 Spring 中配置 SessionFactory 实例

        
        
        


    > 在 Spring 中配置使用声明式事务。
        * 配置事务管理器
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            

    * 配置事务的注解支持


5). 加入struts2
> 加入jar包
> 在web.xml文件中配置struts2


        struts2        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter        


    
        struts2
        /*

> 在 Spring 的配置文件中配置 Struts2 的 Action 实例,id为action名,class为全类名,
基于注解的
@Controller
public class CustomerAction extends ActionSupport

> 配置struts.xml文件,其中的在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID

            /success.jsp


具体的基于注解的spring配置
applicationContext.xml

    xmlns:xsi=""
    xmlns:aop=""
    xmlns:context=""
    xmlns:tx=""
    xsi:schemaLocation="
        
        
         ">
    
    
    
    
    
        
        
        
        
    

    
    
        
        
        
    

    
             class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            
    

    



二:基于xml的配置:只是在applicationContext.xml 中修改些数据即可
1.不再需要
2.把注解的bean节点在applicationContext中配置
3.配置事务

            
        

    
    
        
            
            
        

    

    

    
    
    


具体的基于xml的spring配置
applicationContext.xml:

    xmlns:xsi=""
    xmlns:aop=""
    xmlns:context=""
    xmlns:tx=""
    xsi:schemaLocation="
        
        
         ">
    
    
    
    
    
        
        
        
        
    

    
    
    
        
        
        
    

    
    
        
    

    
    
        
    

    
    
        
    

    
    

        
            
        

    
    
            
                
                
            

    

    
    
        
        
    
    



SSH的整合


Struts2 , Hibernate , Spring 都已经讲完,3个框架各司其职,给了我们很大的方便;框架也算是一种软件,只是更加的底层,我们是在别人的基础上再次的开发新的软件。

此刻对ssh的理解:Hibernate ,搬运工,进行数据的存取;Struts2,转运工,解释数据的来龙去脉;Spring , 监工,hold全场;而我们自己写的Action是操作工,对数据进行需要的处理

此处记录SSH整合的基本配置,对于Spring的配置有两种:一种是基于注解的,我们不在xml文件中配置我们写的java类的bean属性,而是在 java类中通过注解来进行标示;另一种是基于xml文件的,我们在spring的配置文件中,手动的配置bean属性,确定bean的id,class 和param。相比较而言,基于xml的配置维护起来更为方便明了



一:基于注解    

1). 加入 Spring
    > 加入 jar 包
org.springframework.aop-3.1.0.M1.jar:spring的面向切面编程,提供aop实现
.asm-3.1.0.M1.jar:spring独立的asm程序,相比2.5版本,需要额外的asm.jar包
.aspects-3.1.0.M1.jar:spring提供AspectJ框架的整合
.beans-3.1.0.M1.jar:spring IOC的基础实现
.context-3.1.0.M1.jar:spring提供在基础IOC功能上的扩展服务。提供企业级服务的支持
.context.support-3.1.0.M1.jar:spring-context的扩展支持,用于mvc方面
.core-3.1.0.M1.jar:spring3.1的核心工具包
.expression-3.1.0.M1.jar:spring的表达式语言
.instrument.tomcat-3.1.0.M1.jar:spring3.1对Tomcat的连接池的集成
.instrument-3.1.0.M1.jar:spring3.1对服务器的代理接口
.jdbc-3.1.0.M1.jar:spring对jdbc的简单封装
.jms-3.1.0.M1.jar:spring为简化JMS API 使用而做的简单封装
.orm-3.1.0.M1.jar:spring整合第三方的ORM映射支持,如Hibernate,Ibatis
.oxm-3.1.0.M1.jar:spring对Object/XML的映射的支持,可以让java与xml来回切换
.test-3.1.0.M1.jar:spring对Junit的测试框架的简单封装
.transaction-3.1.0.M1.jar:为jdbc,hibernate,jdo,jpa等提供的一致的声明式和编程式事务管理
.web.portlet-3.1.0.M1.jar:spring mvc的增强
.web.servlet-3.1.0.M1.jar:对j2ee6.0 Servlet3.0的支持
.web.struts-3.1.0.M1.jar:整合struts的支持
.web-3.1.0.M1.jar:spring web下的工具包

其中红色的一般是必须需要的,以.开头的前面是org.springframework
为了方便我们也把相关的jar包添加进来
commons-logging-1.1.1.jar:spring的事务依赖此包,日志
c3p0-0.9.1.2.jar:数据源
mysql-connector-java-5.1.7-bin.jar:mysql的jar包
com.springsource.org.aopalliance-1.0.0.jar:aop实现的辅助
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar:aop实现的辅助

    > 加入 Spring 的配置文件
applicationContext.xml
> 配置 Spring IOC 容器自动扫描的包(基于注解才需要)
在applicationContext.xml 文件中配置
    

    > 配置 web.xml 文件.
在 Web 应用的 web.xml 文件中声明一个 ContextLoaderListener 并且在同一文件里增加一个 contextConfigLocation , 这个声明决定了哪些 Spring XML 配置文件将要被加载。
    
    
        contextConfigLocation
        classpath:applicationContext*.xml
    

    
    
    org.springframework.web.context.ContextLoaderListener
    

    若没指定 contextConfigLocation 的context参数, ContextLoaderListener 将会寻找一个名为 /WEB-INF/applicationContext.xml 的文件以加载。 一旦context文件被加载,Spring 通过文件中 bean 的定义创建一个 WebApplicationContext 对象并且将它储存在 Web 应用的 ServletContext 中。
所有 Java Web 框架都构建在 Servlet API 之上,所以可以使用下面的代码片断访问这个 由 ContextLoaderListener 创建的ApplicationContext。
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
2). 在 Spring 中配置 dataSource,资源化文件
在类路径下添加jdbc.properties文件,连接数据库所需的信息
user=root
password=198921
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///jin

    > 加入外置化配置的 properties 文件
在applicationContext.xml 文件中配置



配置dataSource

        
        
        
        
    

3). 加入 Hibernate
    > 加入 jar 包
> 加入 Hibernate 的 XXX.hbm.xml 文件.
    > 加入 Hibernate 的 hibernate.cfg.xml 文件.
    在类路径中加入hibernate.cfg.xml 文件

    
    
        
        200
        
        50
        true
        true
        org.hibernate.dialect.MySQLInnoDBDialect
        
        update
        
        
    



4). Spring 整合 Hibernate.    
    > 在 Spring 中配置 SessionFactory 实例

        
        
        


    > 在 Spring 中配置使用声明式事务。
        * 配置事务管理器
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            

    * 配置事务的注解支持


5). 加入struts2
> 加入jar包
> 在web.xml文件中配置struts2


        struts2        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter        


    
        struts2
        /*

> 在 Spring 的配置文件中配置 Struts2 的 Action 实例,id为action名,class为全类名,
基于注解的
@Controller
public class CustomerAction extends ActionSupport

> 配置struts.xml文件,其中的在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID

            /success.jsp


具体的基于注解的spring配置
applicationContext.xml

    xmlns:xsi=""
    xmlns:aop=""
    xmlns:context=""
    xmlns:tx=""
    xsi:schemaLocation="
        
        
         ">
    
    
    
    
    
        
        
        
        
    

    
    
        
        
        
    

    
             class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            
    

    



二:基于xml的配置:只是在applicationContext.xml 中修改些数据即可
1.不再需要
2.把注解的bean节点在applicationContext中配置
3.配置事务

            
        

    
    
        
            
            
        

    

    

    
    
    


具体的基于xml的spring配置
applicationContext.xml:

    xmlns:xsi=""
    xmlns:aop=""
    xmlns:context=""
    xmlns:tx=""
    xsi:schemaLocation="
        
        
         ">
    
    
    
    
    
        
        
        
        
    

    
    
    
        
        
        
    

    
    
        
    

    
    
        
    

    
    
        
    

    
    

        
            
        

    
    
            
                
                
            

    

    
    
        
        
    
    

原文地址:

阅读(311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~