配置WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--spring 配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application-config.xml
</param-value>
</context-param>
</web-app>
application-config.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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 指定配置文件--类路径>
<value>classpath:/config/db.properties</value>
<!-- 指定配置文件--文件路径>
<value>file:/home/data/x.properties</value>
<!-- 指定配置文件--WEB-ROOT下路径>
<value>/WEB-INF/config/jdbc.properties</value>
</list>
</property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.jdbc.driverClassName}"/>
<property name="url" value="${db.jdbc.url}"/>
<property name="username" value="${db.jdbc.username}"/>
<property name="password" value="${db.jdbc.password}"/>
</bean>
<!--配置事务管理类-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceMpm"></property>
</bean>
<!--事务注解方式-->
<tx:annotation-driven transaction-manager="dataSource" />
<!---DAO-->
<bean name="taskDao" class="com.test.task.dao.TaskDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--Service-->
<bean name="taskService" class="com.test.task.service.TaskService">
<property name="taskDao" ref="taskDao"></property>
</bean>
</beans>
类上事务的声明:
@Transactional(rollbackFor=Exception.class)
//标注类中每一个方法都会启用事务,
// rollbackFor=Exception.class:指定当方法抛出Exception时事务回滚。
//@Transactional()未指定rollbackFor时,默认为当抛出RuntimeException时才事务回滚
public class TaskService {
@Transactional(rollbackFor=RuntimeException.class)
//以此事务形式启用事务,忽略类上声明的事务形式
public void triggerTask(){
}
//启用事务,继承类上定义的事务形式(@Transactional(rollbackFor=Exception.class))
public void triggerGeneratorTask(){
}
@Transactional(propagation= Propagation.NOT_SUPPORTED,readOnly=true)
//不启用事务
public List getRunningTask(){
return null;
}
}