①:spring和hibernate整合
1.hibernate和spring在整合的时候推荐把jdbc的配置单独写在一个jdbc.properties文件里,然后在spring文件中以${userName}形式引用(hibernate是不具备这样引用的功能)
2.hibernate和spring整合主要目的就是:1.让spring来管理sessionFactory,提供了管理事务的功能。
步骤如下:
1.jdbc.properties:
jdbcUrl = jdbc:mysql://localhost:3306/OA_ANNOTATION
driverClass = com.mysql.jdbc.Driver
username = root
password = root
2.hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接信息,帐号密码不推荐在这里配置,而是配置在一个properties中,由spring的配置文件进行引用(hibernate不支持这样的引用) -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 其他配置信息 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- 声明映射文件 -->
<mapping resource="com/xxc/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.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: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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 导入外部的properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- =========== 数据库连接信息 =========== -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- =========== 连接池的管理配置 =========== -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 直接配置hibernate配置,缺点:数据库的连接信息也写在hiberntae,修改起来不方便
推荐写法是:1.将数据库连接信息写在一个properties文件中,然后由spring文件以${username}形式引用(hibernate不支持这种引用方式,而且配置数据库的信息比较少)
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property>
-->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"></property><!-- 引入hibernate配置文件 -->
</bean>
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="com.xxc.oa"></context:component-scan>
<!-- 配置声明式的事务管理(采用基于注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务注解解析器 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
②.spring和struts2整合就很简单了:
只要引入struts2-spring-plugin-2.1.8.1.jar,这个jar包中自带struts-plugin.xml,所以根据struts2的工作流程可知(按先后顺序加载default.properties--->struts-default.xml--->struts-plugin.xml--->struts.xml),struts2在启动的时候会自动加载jar包中的
struts-plugin.xml文件。这样就完成了spring和struts2整合。
③.最后,由于平时junit测试的时候对于spring容器我们都是手动创建的:ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
而当spring处于web项目的时候,spring容器是不再需要我们自己手动创建,而是交给web容器创建,所以在web.xml中还需要配置一个监听器:
<!--
当在web环境下使用spring的时候需要
配置Spring用于初始化ApplicationContext对象的监听器
当没在web环境下运行的时候ApplicationContext是靠
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
这样的形式手动new的
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>