1. 程式人生 > >ssh spring.xml相關配置

ssh spring.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: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		<!-- 配置資料來源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="user" value="scott"></property>
		<property name="password" value="tiger"></property>
		<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
		<!--配置連線池初始值 -->
		      <!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
		<property name="acquireIncrement" value="2"></property>
		<!--初始化時獲取的連線數,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
		<property name="initialPoolSize" value="30"></property>
		<!--當最少空閒連線時,連線數小於該值,就會自動申請一些連線 -->
		 <property name="minPoolSize" value="5"></property>
		    <!--連線池中保留的最大連線數。Default: 15 -->                                           
		<property name="maxPoolSize" value="200"></property>
		 <!--最大空閒時間,60秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->
		<!-- <property name="maxIdleTime" value="1000"></property>  -->
	</bean>
	<!-- 生成SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 使用某個資料來源生成SessionFactory -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置Hibernate的相關屬性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.formate_sql">true</prop>
				<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/zhiyou100/entity/Users.hbm.xml</value>
				<value>com/zhiyou100/entity/Provider.hbm.xml</value>
				<value>com/zhiyou100/entity/Bill.hbm.xml</value>
			</list>
		</property>
	</bean>

	<bean id="userDao" class="com.zhiyou100.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="proDao" class="com.zhiyou100.dao.impl.ProDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="billDao" class="com.zhiyou100.dao.impl.BillDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	
<!-- 配置事務管理器 --> 
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
<!-- 配置事務管理的通知 -->
	<tx:advice id="tx" transaction-manager="transactionManager">
		<tx:attributes>
	<!-- 本操作內如果有事務,則使用事務,如果沒有,則開啟新的事務 -->
			<tx:method name="user*" read-only="false" propagation="REQUIRED" />
			<tx:method name="delete*" read-only="false" propagation="REQUIRED" />
			<tx:method name="update*" read-only="false" propagation="REQUIRED" />
			<tx:method name="get*ById" read-only="false" propagation="REQUIRED" />
			<tx:method name="add*" read-only="false" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
<!-- 切面配置 -->
	<aop:config>
		<aop:pointcut
			expression="execution (* com.zhiyou100.service.impl.*.*(..))" id="perform" />
		<aop:advisor advice-ref="tx" pointcut-ref="perform" />
	</aop:config>
	 <context:component-scan base-package="com.zhiyou100"></context:component-scan> 
</beans>