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 文件中配置
<context:component-scan base-package="jin.ssh2"></context:component-scan>
> 配置 web.xml 文件.
在 Web 应用的 web.xml 文件中声明一个 ContextLoaderListener 并且在同一文件里增加一个 contextConfigLocation <context-param/> , 这个声明决定了哪些 Spring XML 配置文件将要被加载。
<!-- 配置 Spring 的配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 配置初始化 Spring IOC 容器的 Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
若没指定 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 文件中配置
<context:property-placeholder location="classpath:jdbc.properties"/>
配置dataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
3). 加入 Hibernate
> 加入 jar 包
> 加入 Hibernate 的 XXX.hbm.xml 文件.
> 加入 Hibernate 的 hibernate.cfg.xml 文件.
在类路径中加入hibernate.cfg.xml 文件
<hibernate-configuration>
<session-factory>
<!-- 连接数据库的基本信息 -->
<!-- 设置批量抓取记录的数量 -->
<property name="jdbc.fetch_size">200</property>
<!-- 设置批量处理的数量 -->
<property name="jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 配置生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定当前 Hibernate 程序关联的映射文件 -->
<mapping resource="jin/ssh2/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4). Spring 整合 Hibernate.
> 在 Spring 中配置 SessionFactory 实例
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
> 在 Spring 中配置使用声明式事务。
* 配置事务管理器
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
* 配置事务的注解支持
<tx:annotation-driven transaction-manager="transactionManager"/>
5). 加入struts2
> 加入jar包
> 在web.xml文件中配置struts2
<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>
> 在 Spring 的配置文件中配置 Struts2 的 Action 实例,id为action名,class为全类名,
基于注解的
@Controller
public class CustomerAction extends ActionSupport
> 配置struts.xml文件,其中的在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID
<action name="testssh" class="customerAction">
<result>/success.jsp</result>
</action>
具体的基于注解的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="jin.ssh2"></context:component-scan>
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
二:基于xml的配置:只是在applicationContext.xml 中修改些数据即可
1.<context:component-scan base-package="jin.ssh2"></context:component-scan>不再需要
2.把注解的bean节点在applicationContext中配置
3.配置事务
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
具体的基于xml的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- <context:component-scan base-package="jin.ssh"></context:component-scan> -->
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="customerDAO" class="jin.ssh.CustomerDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="customerService" class="jin.ssh.CustomerServiceImpl">
<property name="customerDAO" ref="customerDAO"></property>
</bean>
<bean id="customerAction" class="jin.ssh.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<!-- 配置 Spring 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
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 文件中配置
<context:component-scan base-package="jin.ssh2"></context:component-scan>
> 配置 web.xml 文件.
在 Web 应用的 web.xml 文件中声明一个 ContextLoaderListener 并且在同一文件里增加一个 contextConfigLocation <context-param/> , 这个声明决定了哪些 Spring XML 配置文件将要被加载。
<!-- 配置 Spring 的配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 配置初始化 Spring IOC 容器的 Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
若没指定 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 文件中配置
<context:property-placeholder location="classpath:jdbc.properties"/>
配置dataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
3). 加入 Hibernate
> 加入 jar 包
> 加入 Hibernate 的 XXX.hbm.xml 文件.
> 加入 Hibernate 的 hibernate.cfg.xml 文件.
在类路径中加入hibernate.cfg.xml 文件
<hibernate-configuration>
<session-factory>
<!-- 连接数据库的基本信息 -->
<!-- 设置批量抓取记录的数量 -->
<property name="jdbc.fetch_size">200</property>
<!-- 设置批量处理的数量 -->
<property name="jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 配置生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定当前 Hibernate 程序关联的映射文件 -->
<mapping resource="jin/ssh2/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4). Spring 整合 Hibernate.
> 在 Spring 中配置 SessionFactory 实例
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
> 在 Spring 中配置使用声明式事务。
* 配置事务管理器
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
* 配置事务的注解支持
<tx:annotation-driven transaction-manager="transactionManager"/>
5). 加入struts2
> 加入jar包
> 在web.xml文件中配置struts2
<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>
> 在 Spring 的配置文件中配置 Struts2 的 Action 实例,id为action名,class为全类名,
基于注解的
@Controller
public class CustomerAction extends ActionSupport
> 配置struts.xml文件,其中的在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID
<action name="testssh" class="customerAction">
<result>/success.jsp</result>
</action>
具体的基于注解的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="jin.ssh2"></context:component-scan>
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
二:基于xml的配置:只是在applicationContext.xml 中修改些数据即可
1.<context:component-scan base-package="jin.ssh2"></context:component-scan>不再需要
2.把注解的bean节点在applicationContext中配置
3.配置事务
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
具体的基于xml的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- <context:component-scan base-package="jin.ssh"></context:component-scan> -->
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="customerDAO" class="jin.ssh.CustomerDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="customerService" class="jin.ssh.CustomerServiceImpl">
<property name="customerDAO" ref="customerDAO"></property>
</bean>
<bean id="customerAction" class="jin.ssh.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<!-- 配置 Spring 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
此刻对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 文件中配置
<context:component-scan base-package="jin.ssh2"></context:component-scan>
> 配置 web.xml 文件.
在 Web 应用的 web.xml 文件中声明一个 ContextLoaderListener 并且在同一文件里增加一个 contextConfigLocation <context-param/> , 这个声明决定了哪些 Spring XML 配置文件将要被加载。
<!-- 配置 Spring 的配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 配置初始化 Spring IOC 容器的 Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
若没指定 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 文件中配置
<context:property-placeholder location="classpath:jdbc.properties"/>
配置dataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
3). 加入 Hibernate
> 加入 jar 包
> 加入 Hibernate 的 XXX.hbm.xml 文件.
> 加入 Hibernate 的 hibernate.cfg.xml 文件.
在类路径中加入hibernate.cfg.xml 文件
<hibernate-configuration>
<session-factory>
<!-- 连接数据库的基本信息 -->
<!-- 设置批量抓取记录的数量 -->
<property name="jdbc.fetch_size">200</property>
<!-- 设置批量处理的数量 -->
<property name="jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 配置生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定当前 Hibernate 程序关联的映射文件 -->
<mapping resource="jin/ssh2/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4). Spring 整合 Hibernate.
> 在 Spring 中配置 SessionFactory 实例
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
> 在 Spring 中配置使用声明式事务。
* 配置事务管理器
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
* 配置事务的注解支持
<tx:annotation-driven transaction-manager="transactionManager"/>
5). 加入struts2
> 加入jar包
> 在web.xml文件中配置struts2
<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>
> 在 Spring 的配置文件中配置 Struts2 的 Action 实例,id为action名,class为全类名,
基于注解的
@Controller
public class CustomerAction extends ActionSupport
> 配置struts.xml文件,其中的在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID
<action name="testssh" class="customerAction">
<result>/success.jsp</result>
</action>
具体的基于注解的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="jin.ssh2"></context:component-scan>
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
二:基于xml的配置:只是在applicationContext.xml 中修改些数据即可
1.<context:component-scan base-package="jin.ssh2"></context:component-scan>不再需要
2.把注解的bean节点在applicationContext中配置
3.配置事务
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
具体的基于xml的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- <context:component-scan base-package="jin.ssh"></context:component-scan> -->
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="customerDAO" class="jin.ssh.CustomerDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="customerService" class="jin.ssh.CustomerServiceImpl">
<property name="customerDAO" ref="customerDAO"></property>
</bean>
<bean id="customerAction" class="jin.ssh.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<!-- 配置 Spring 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
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 文件中配置
<context:component-scan base-package="jin.ssh2"></context:component-scan>
> 配置 web.xml 文件.
在 Web 应用的 web.xml 文件中声明一个 ContextLoaderListener 并且在同一文件里增加一个 contextConfigLocation <context-param/> , 这个声明决定了哪些 Spring XML 配置文件将要被加载。
<!-- 配置 Spring 的配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 配置初始化 Spring IOC 容器的 Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
若没指定 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 文件中配置
<context:property-placeholder location="classpath:jdbc.properties"/>
配置dataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
3). 加入 Hibernate
> 加入 jar 包
> 加入 Hibernate 的 XXX.hbm.xml 文件.
> 加入 Hibernate 的 hibernate.cfg.xml 文件.
在类路径中加入hibernate.cfg.xml 文件
<hibernate-configuration>
<session-factory>
<!-- 连接数据库的基本信息 -->
<!-- 设置批量抓取记录的数量 -->
<property name="jdbc.fetch_size">200</property>
<!-- 设置批量处理的数量 -->
<property name="jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 配置生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定当前 Hibernate 程序关联的映射文件 -->
<mapping resource="jin/ssh2/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4). Spring 整合 Hibernate.
> 在 Spring 中配置 SessionFactory 实例
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
> 在 Spring 中配置使用声明式事务。
* 配置事务管理器
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
* 配置事务的注解支持
<tx:annotation-driven transaction-manager="transactionManager"/>
5). 加入struts2
> 加入jar包
> 在web.xml文件中配置struts2
<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>
> 在 Spring 的配置文件中配置 Struts2 的 Action 实例,id为action名,class为全类名,
基于注解的
@Controller
public class CustomerAction extends ActionSupport
> 配置struts.xml文件,其中的在 Struts 配置文件中配置 action, 但其 class 属性不再指向该 Action 的实现类, 而是指向 Spring 容器中 Action 实例的 ID
<action name="testssh" class="customerAction">
<result>/success.jsp</result>
</action>
具体的基于注解的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="jin.ssh2"></context:component-scan>
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
二:基于xml的配置:只是在applicationContext.xml 中修改些数据即可
1.<context:component-scan base-package="jin.ssh2"></context:component-scan>不再需要
2.把注解的bean节点在applicationContext中配置
3.配置事务
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
具体的基于xml的spring配置
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- <context:component-scan base-package="jin.ssh"></context:component-scan> -->
<!-- 配置外置化的资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 整合 Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- dataSource 会覆盖hibernate配置文件中的数据库配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="customerDAO" class="jin.ssh.CustomerDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="customerService" class="jin.ssh.CustomerServiceImpl">
<property name="customerDAO" ref="customerDAO"></property>
</bean>
<bean id="customerAction" class="jin.ssh.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<!-- 配置 Spring 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- execution表达式中的类只能是实现接口的类 -->
<aop:pointcut id="txPointcut" expression="execution(* jin.ssh.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>