您的位置:首页 > 编程语言 > Java开发

SSH框架整合之Spring和Hibernate整合

2013-12-15 21:12 453 查看
今天简单地整合了一下SSH框架,由于整合一次估计下次整合都不知道是何年何月了。所以决定做点笔记,免得下次忘记。由于SSH整合之后,Dao层,Model层,Controller层的对象均可以通过Spring的注入来初始化,同时Spring中也封装了一下Hibernate,所以Hibernate.cfg.xml文件不再需要了。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 
<context:annotation-config />
<context:component-scan base-package="com.ssh" />
<aop:aspectj-autoproxy />

<!--
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName"
value="com.mysql.jdbc.Driver" /> <property name="url"
value="jdbc:mysql://localhost:3306/db_spring" /> <property
name="username" value="root" /> <property name="password" value="123"
/> </bean>
-->

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<!--
<property name="annotatedClasses"> <list>
<value>com.sram.model.User</value> <value>com.sram.model.Log</value>
</list> </property>
-->
<property name="packagesToScan">
<list>
<value>com.ssh.model</value>
</list>
</property>

<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>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<aop:config>

<aop:pointcut id="entryPointMethod"
expression="execution(public * com.ssh.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="entryPointMethod" />

</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

</beans>


先对这个配置文件做一些说明。

(1)、下面两句配置主要说明,spring会扫描com.ssh下面包的所有类来通过注解来注入对象,使对象初始化

<context:annotation-config />
<context:component-scan base-package="com.ssh" />

(2)、下面的配置为连接数据源的参数设置

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

这部分配置主要说明了应用程序通过dbcp来连接数据源。以及一些连接参数设置。其中jdbc.properties的内容如下:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_ssh
jdbc.username=root
jdbc.password=123


(2)、下面这部分配置主要是说明sessionFactory的注入

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="packagesToScan">
<list>
<value>com.ssh.model</value>
</list>
</property>

<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>
</bean>


最后注入hibernateTemplate就可以直接使用了,最终通过声明式事务,把session和sessionFactory全部交由Spring来进行管理。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: