天天看點

SSH架構整合之Spring和Hibernate整合

       今天簡單地整合了一下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來進行管理。



繼續閱讀