日期:2014-05-19  浏览次数:20560 次

J2EE SSH2架构(2)

该架构是设计的第二个架构,和第一个不同的是,采用了AOP事务管理方式,代码如下:

<?xml version="1.0" encoding="GBK"?>
<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:tx="http://www.springframework.org/schema/tx"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 采用bean方式配置数据源和sessionfactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="hibernateProperties" ref="hibernateProperties" />
		<property name="dataSource" ref="dataSource" />
		<!-- 
		<property name="configLocation">
			<value>classpath:hiberante.cfg.xml</value>
		</property>
		 -->
		<property name="packagesToScan">
			<list>
				<value>com.bing.entity</value>
			</list>
		</property>
	</bean>
	<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/ssh2" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
<!-- 采用AOP(面向方面编程)方式配置事物 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>

	<!-- 哪些类的哪些方法参与事物, (* com.evan.crm.service.*.*(..))中几个通配符的含义:
		第一个 * —— 通配 任意返回值类型
		第二个 * —— 通配 包com.evan.crm.service下的任意class
		第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法
		第四个 .. —— 通配 方法可以有0个或多个参数
		综上:包com.evan.crm.service下的任意class的具有任意返回值类型、任意数目参数和任意名称的方法-->
	<aop:config>
		<aop:pointcut id="allManagerMethod"
			expression="execution(* com.bing.dao.*.*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod"
			advice-ref="txAdvice" />
	</aop:config>
	<!-- Clob 类型处理配置 BEGIN -->
	<bean id="defaultLobHandler" lazy-init="true"
		class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

	<!-- hibernate 基本配置  -->
	<bean id="hibernateProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="properties">
			<props>
				<prop key="hibernate.connection.charSet">UTF-8</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
</beans>

?

文件夹分布图:

?

?

在文件结构上,和Module1并无太大区别,区别就在spring-hibernate.xml文件中,即上面贴出的代码中。并且采用的是AnnotationSessionFactoryBean管理OR实体映射,省去了编写配置文件的问题。

附件地址:

http://download.csdn.net/source/3014114