一.xml配置
[code]
applicationContext.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!– 进行aop的配置 advice-ref="tx" 指向通知 pointcut-ref="preform" 指向切入点表达式 –>
<aop:config>
<aop:pointcut
expression="execution(* cn.ytu.aop.jdbc.transaction.xml.PersonServiceImpl.*(..))"
id="preform" />
<aop:advisor advice-ref="tx" pointcut-ref="preform" />
</aop:config>
<!– 配置通知 –>
<!– id="tx" 通知的唯一标示 transaction-manager="transactionManager" –>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<!–name="save*"方法的名称。 save* 表示Sava开头的方法。 第二个*表示除去上面第一个表示的方法外的任意方法 isolation="DEFAULT"
事务的隔离机制 propagation="REQUIRED" 事务的广播机制 read-only="false" 数据的只读属性的设置。 一般情况下查询设置为true,其他设置为false –>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false" />
</tx:attributes>
</tx:advice>
<!–引入事务管理器 用于告诉spring容器用哪种技术进行事务管理 –>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!– 引入数据源dataSource dbcp数据池 –>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!– 指明数据库配置的properity文件 –>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>
classpath:cn/ytu/aop/jdbc/transaction/xml/jdbc.properties
</value>
</property>
</bean>
<!– 注入service –>
<bean id="personService" class="cn.ytu.aop.jdbc.transaction.xml.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao" />
</property>
</bean>
<!– 注入persondao –>
<bean id="personDao" class="cn.ytu.aop.jdbc.transaction.xml.PersonDaoImpl">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>
>
[/code]
二.
(一)注解方式
配置文件 applicationContext.xml
[code]
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!–引入事务管理器 用于告诉spring容器用哪种技术进行事务管理 –>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!– 引入数据源dataSource dbcp数据池 –>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!– 指明数据库配置的properity文件 –>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>
classpath:cn/ytu/aop/jdbc/transaction/xml/jdbc.properties
</value>
</property>
</bean>
<!– 类扫描的注解解析器 –>
<context:component-scan base-package="cn.ytu.aop.jdbc.transaction.annocation"></context:component-scan>
<!– 事务的注解解析器 transaction-manager="transactionManager" 事务管理器 –>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>
[/code]
数据库接口PersonDao
[code]
public interface PersonDao {
void insertPerson(Person person);
}
[/code]
数据库实现PersonDaoImpl
[code]
@Repository(value="personDao")
public class PersonDaoImpl implements PersonDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Override
public void insertPerson(Person person) {
this.jdbcTemplate.update("insert into person(id,name) values (?,?)", new Object[]{person.getId(),person.getName()});
}
}
[/code]
Service接口PersonService
[code]
public interface PersonService {
void insertPerson(Person p);
}
[/code]
Service的实现PersonServiceImpl
[code]
@Service(value="personService")
public class PersonServiceImpl implements PersonService {
@Resource(name="personDao")
private PersonDao personDao;
@Override
public void insertPerson(Person p) {
this.personDao.insertPerson(p);
}
}
[/code]