数据源:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/study"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
数据源也可以用以下配置:
jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.driver=driverClassName" value="com.mysql.jdbc.Driver
#jdbc.url=jdbc\:mysql\://localhost\:3308/ictjc
#jdbc.username=username
#jdbc.password=password
jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:ORCL
jdbc.username=username
jdbc.password=password
jdbc.useUnicode=true
jdbc.characterEncoding=UTF-8
<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="connectionProperties">
<props>
<prop key="useUnicode">${jdbc.useUnicode}</prop>
<prop key="characterEncoding">${jdbc.characterEncoding}</prop>
</props>
</property>
</bean>
事务管理者:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
被管理的对象:
<aop:config>
<aop:advisor advice-ref="wrapperTxAdvice" pointcut="execution(public * com.jsict.ismpb.service.impl.*.*(..))" />
</aop:config>
事务性代理对象:
<tx:advice id="wrapperTxAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" read-only="false" timeout="25" />
<tx:method name="updata*" propagation="REQUIRED" read-only="false" timeout="25" />
<tx:method name="*" propagation="REQUIRED" timeout="25"/>
</tx:attributes>
</tx:advice>
说明:-MyCheckedException表示抛出此导常时回滚,+MyCheckedException表示抛出此导常时提交。
MyBusinessObject:
package mybusiness;
public class MyBusinessObject {
public void myBusinessMethod() throws MyNameException {
// do some resource access
// return some result object or throw MyNameException
}
}
声明式事务的其中一种形式。