`

以前在购物车项目写的spring配置文件和spring简单整合struts的文件

阅读更多
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
		<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
		<value>jdbc:mysql://localhost/huanglq</value>
		</property>
		<property name="username">
		<value>root</value>
		</property>
		<property name="password">
		<value>root</value>
		</property>
		</bean>
	<!--bean id="dataSource"
		class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/huanglq</value>
		</property>
	</bean-->

	<!-- 定义Hibernate SessionFactory,在hibernate配置文件中用到的配置项
		都可以移植到此处,包括hbm文件路径、dialect、是否显示sql语句等-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>com/huanglq/vo/EcCategory.hbm.xml</value>
				<value>com/huanglq/vo/EcProduct.hbm.xml</value>
				<value>com/huanglq/vo/EcContactinfo.hbm.xml</value>
				<value>com/huanglq/vo/EcUser.hbm.xml</value>
				<value>com/huanglq/vo/EcOrder.hbm.xml</value>
				<value>com/huanglq/vo/EcOrderline.hbm.xml</value>
				<value>com/huanglq/vo/EcPayway.hbm.xml</value>
				<value>com/huanglq/vo/EcOrderstatus.hbm.xml</value>
			</list>
		</property>
<!--可以把上面的一堆改为下面的形式
<property name="mappingDirectoryLocations">
			<list>
				<value>classpath*:/com/huanglq/vo/</value>
			</list>
		</property>
-->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQL5Dialect
				</prop>
				<prop key="hibernate.query.substitutions">
					true 'Y', false 'N'
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>

	<!--定义事务管理器  -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 定义事务的处理的拦截代理器 -->
	<!-- 
		1、PROPAGATION_REQUIRED :支持当前的事务,如果不存在就创建一个新的。这是最常用的选择。
		2、PROPAGATION_SUPPORTS :支持当前的事务,如果不存在就不使用事务。
		3、PROPAGATION_MANDATORY :支持当前的事务,如果不存在就抛出异常。
		4、PROPAGATION_REQUIRES_NEW :创建一个新的事务,并暂停当前的事务(如果存在)。
		5、PROPAGATION_NOT_SUPPORTED :不使用事务,并暂停当前的事务(如果存在)。
		6、PROPAGATION_NEVER :不使用事务,如果当前存在事务就抛出异常。
		7、PROPAGATION_NESTED :如果当前存在事务就作为嵌入事务执行,否则与PROPAGATION_REQUIRED类似。
	-->
	<bean id="baseTransactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		abstract="true"><!--允许子类-->
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="modify*">PROPAGATION_REQUIRED</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>

	<!-- 配置产品Dao -->
	<bean id="productDao" class="com.huanglq.dao.ProductHDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 配置产品Bo -->
	<bean id="productBo" class="com.huanglq.bo.ProductBusinessImpl">
		<property name="productDao" ref="productDao" />
	</bean>
	<!-- 可以配置多个业务逻辑对象的事务代理器 -->
	<!-- 在dao的级别进行事务管理 -->
	<bean id="productDaoProxy" parent="baseTransactionProxy">
		<property name="target" ref="productDao" />
	</bean>

	<!-- 配置用户dao -->
	<bean id="userDao" class="com.huanglq.dao.UserHDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 配置用户Bo -->
	<bean id="userBo" class="com.huanglq.bo.UserBusinessImpl">
		<property name="userDao" ref="userDao" />
	</bean>
	<!-- 在dao级别进行事务管理 -->
	<bean id="userDaoProxy" parent="baseTransactionProxy">
		<property name="target" ref="userDao" />
	</bean>

	<!-- 不同的用户必须使用不同的购物车 -->
	<!-- 购物车应该是在session被创建的时候才创建 -->
	<!-- 购物车不应该由Spring管理 -->
	<!-- Sesion被创建的时候,必须创建购物车,可以使用SessionListener -->
	<bean id="shoppingCartBo" class="com.huanglq.bo.ShoppingCartImpl">
	</bean>


	<!-- 订单管理 -->
	<bean id="orderDao" class="com.huanglq.dao.OrderHDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="orderBo" class="com.huanglq.bo.OrderBusinessImpl">
		<property name="orderDao" ref="orderDao" />
	</bean>

	<bean id="orderDaoProxy" parent="baseTransactionProxy">
		<property name="target" ref="orderDao" />
	</bean>
</beans>


下面是spring简单整合struts的文件
用的是spring2.5
spring.jar、spring-webmvc-struts.jar、commons-logging.jar

struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
	<form-beans>
		<form-bean name="userForm"
			type="com.huanglq.action.form.UserForm" />

	</form-beans>

	<global-exceptions />
	<global-forwards />
	<action-mappings>

		<action path="/user"
			type="org.springframework.web.struts.DelegatingActionProxy"
			attribute="userForm" name="userForm" scope="request"
			parameter="method">
			<forward name="success" path="/success.jsp" />
			<forward name="fail" path="/user/login.jsp" />
		</action>
	</action-mappings>

	<message-resources parameter="com.huanglq.resource.ApplicationResources" />

	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
	</plug-in>
</struts-config>



applicationContext.xml(配置了c3p0)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


	<bean id="dataSourse"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/ssh_example" />
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>

		<!--连接池中保留的最小连接数。-->
		<property name="minPoolSize">
			<value>5</value>
		</property>

		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize">
			<value>30</value>
		</property>

		<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize">
			<value>10</value>
		</property>

		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement">
			<value>5</value>
		</property>

		<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements   属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。   如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
		<property name="maxStatements">
			<value>0</value>
		</property>

		<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
		<property name="idleConnectionTestPeriod">
			<value>60</value>
		</property>

		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts">
			<value>30</value>
		</property>

		<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效   保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试   获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
		<property name="breakAfterAcquireFailure">
			<value>true</value>
		</property>

		<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的   时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable   等方法来提升连接测试的性能。Default: false -->
		<property name="testConnectionOnCheckout">
			<value>false</value>
		</property>

	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSourse" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.generate_statistics">true</prop>
				<prop key="hibernate.connection.release_mode">
					auto
				</prop>
				<prop key="hibernate.autoReconnect">true</prop>
				<prop key="hibernate.jdbc.batch_size">50</prop>
			</props>
		</property>

		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath*:/com/huanglq/pojo/</value>
			</list>
		</property>
	</bean>

	<bean id="UserDAO" class="com.huanglq.dao.UserDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<bean name="/user" class="com.huanglq.action.UserAction">
		<property name="userDao">
			<ref bean="UserDAO" />
		</property>
	</bean>
</beans>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics