docs:API和开发规范
libs:包 --> .jar包 javadoc.jar 文档 sources.jar 源码
schema:约束
1.导包
日志包
com.springsource.org.apache.commons.logging-1.1.1.jar
可选日志包(老版本)
com.springsource.org.apache.log4j-1.2.15.jar
package com.sjtu.bean;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.书写配置注册对象到容器
建立xml文件,建议放在src下,文件名建议applicationContext.xml
导入约束
Preference–>XML Catalog–>Add–>FileSystem–>Spring目录下的Schema–>beans–>选择最新版
,然后点击左下角的Design,选中beans,右键Edit namespaces ,添加xsi
文件xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
,再点add,选择Specify New Namespace ,选择Browse ,选择 Select XML Catalog entry ,选择刚才导入的 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
,Namespace Name 为http://www.springframework.org/schema/beans
,Prefix为空
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="user" class="com.sjtu.bean.User">bean>
beans>
4.代码测试
@Test
public void fun1() {
//1.创建容器对象,相对于src下的路径
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器“要”user对象
User u=(User) ac.getBean("user");
//3.打印user对象
System.out.println(u);
}
结论:web开发中,使用applicationContext. 在资源匮乏的环境(手机)可以使用BeanFactory.
"1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
"user" class="com.sjtu.bean.User">
"user" class="com.sjtu.bean.User">
//1.创建容器对象,相对于src下的路径
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
public static User createUser() {
System.out.println("静态工厂创建User");//表示由用户自己创建
return new User();
}
<bean name="user2"
class="com.sjtu.b_create.UserFactory" //类的全包名
factory-method="createUser">bean> //类的方法
//测试
public void fun2() {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/sjtu/b_create/applicationContext.xml");
//2.向容器“要”user对象
User u=(User) ac.getBean("user2");
}
public User createUser2() {//不是静态方法
System.out.println("实例工厂创建User");
return new User();
}
<bean name="user3"
factory-bean="userFactory"
factory-method="createUser2">bean>
<bean name="userFactory"
class="com.sjtu.b_create.UserFactory" >bean>
public void fun3() {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/sjtu/b_create/applicationContext.xml");
//2.向容器“要”user对象
User u=(User) ac.getBean("user3");
}
<bean name="user" class="com.sjtu.bean.User" scope="singleton">bean>
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器“要”user对象
User u=(User) ac.getBean("user");
User u2=(User) ac.getBean("user");
User u3=(User) ac.getBean("user");
User u4=(User) ac.getBean("user");
//3.打印user对象 会发现只有一个实例
System.out.println(u==u3);
System.out.println(u2==u4);
init-method
destory-method
<bean name="user" class="com.sjtu.bean.User" scope="singleton" init-method="init" destroy-method="destroy">bean>
//并在User中实现此方法
public void init() {
System.out.println("初始化");
}
public void destroy() {
System.out.println("销毁");
}
主配置文件引入其他配置文件
//在主配置文件中做如下配置
<import resource="com/sjtu/b_create/applicationContext.xml"/>
属于配置方法
<bean name="user" class="com.sjtu.bean.User" >
<property name="name" value="tom">property>
<property name="age" value="18">property>
<property name="car" ref="car">property>
bean>
<bean name="car" class="com.sjtu.bean.Car">
<property name="name" value="兰博基尼">property>
<property name="color" value="黄色">property>
bean>
构造函数注入
准备带有参数的构造
<bean name="user2" class="com.sjtu.bean.User">
<constructor-arg name="name" value="Jerry" index="0" type="java.lang.String">constructor-arg>
<constructor-arg name="car" ref="car">constructor-arg>
bean>
p名称空间注入(了解)
<bean name="user3" class="com.sjtu.bean.User" p:name="jack" p:age="20" p:car-ref="car">
bean>
<bean name="car" class="com.sjtu.bean.Car">
<property name="name" value="兰博基尼">property>
<property name="color" value="黄色">property>
bean>
spel注入(了解)
<bean name="user4" class="com.sjtu.bean.User" >
<property name="name" value="#{user.name}">property>
<property name="age" value="#{user3.age}">property>
<property name="car" ref="car">property>
bean>
public class CollectionBean {
private Object[] arr;//数组类型注入
private List list;//list/set类型注入
private Map map;//map注入
private Properties prop;// Properties 类型注入
}
<bean name="cb" class="com.sjtu.c_injection.CollectionBean">
<property name="arr">
<array>
<value>tomvalue>
<value>jerryvalue>
<ref bean="car"/>
array>
property>
bean>
<property name="list">
<list>
<value>tomvalue>
<value>Jerryvalue>
<ref bean="car"/>
list>
property>
<property name="map">
<map>
<entry key="1" value="abc">entry>
<entry key="2" value="def">entry>
<entry key-ref="car" value-ref="car">entry>
map>
property>
<property name="prop">
<props>
<prop key="abc">abcprop>
<prop key="def">defprop>
<prop key="ghi">ghiprop>
props>
property>
1.为主配置文件引入新的命名空间(约束)
2.开启使用注解代替配置文件
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<context:component-scan base-package="com.sjtu.bean">context:component-scan>
beans>
@Component("user") //这四个注解完全一样,以下三个只是为了分层
@Service("user") //Service层
@Controller("user") //Web层
@Repository("user") //Dao层
public class User {
}
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u=(User) ac.getBean("user");
System.out.println(u);
//指定对象作用范围
@Scope(scopeName="prototype")
public class User {}
//方法一
@Value("Tom")
//加到成员变量上,通过反射的Field赋值,破坏封装性
private String name;
//方法二
@Value("Tom")
//通过set方法赋值
public void setName(String name) {
this.name = name;
}
引用类型引入
@Autowired//自动装配
//问题,如果匹配多个类型一致的对象,将无法选择具体注入哪一对象
@Qualifier("car")//使用@Qualifier注解告诉spring容器自动装配哪个名称的对象
public Car car;
@Resource(name="car") //手动注入,指定注入哪个名称的对象
public Car car;
@Component("car")
public class Car {
@Value("法拉利")
private String name;
@Override
public String toString() {
return "Car [name=" + name + "]";
}
}
初始化、销毁方法
@PostConstruct //在对象被创建之后调用,init-method
public void init() {
System.out.println("初始化");
}
@PreDestroy //在对象销毁之前调用,destroy-method
public void destroy() {
System.out.println("初始化");
}
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定容器使用配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Deom {
//将名为user的对象注入u变量中
@Resource(name="user")
private User u;
@Test
public void fun1() {
System.out.println(u);
}
}
Proxy.newProxyInstance(xx,xx,xx)
生成代理对象,Spring能够帮我们生成代理对象public class UserServiceImpl implements UserService{
@Override
public void save() {
System.out.println("保存");
}
@Override
public void delete() {
System.out.println("删除");
}
@Override
public void update() {
System.out.println("更新");
}
@Override
public void find() {
System.out.println("查找");
}
}
public class MyAdvice {
//前置通知,方法名是自己定义的
public void before() {
System.out.println("这是前置通知!");
}
//后置通知
public void afterReturning() {
System.out.println("这是后置通知(如果出现异常不会调用)");
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分!");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分!");
return proceed;
}
//异常通知
public void afterException() {
System.out.println("异常出现了!");
}
//后置通知
public void after() {
System.out.println("这是后置通知(出现异常也会调用)");
}
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<bean name="userService" class="com.sjtu.proxy.UserServiceImpl">bean>
<bean name="myAdvice" class="com.sjtu.proxy.MyAdvice">bean>
<aop:config>
<aop:pointcut expression="execution(* com.sjtu.proxy..*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pc"/>
<aop:after method="after" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:after-throwing method="afterException" pointcut-ref="pc"/>
<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
aop:aspect>
aop:config>
beans>
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定容器使用配置文件
@ContextConfiguration("classpath:com/sjtu/springaop/applicationContext.xml")
public class Deom {
//将名为user的对象注入u变量中
@Resource(name="userService")
private UserService us;
@Test
public void fun1() {
System.out.println(us);
us.save();
}
}
1.导包
2.准备目标对象
3.准备通知
4.注解
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<bean name="userService" class="com.sjtu.proxy.UserServiceImpl">bean>
<bean name="myAdvice" class="com.sjtu.proxy.MyAdvice">bean>
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
beans>
//表示是一个通知类
@Aspect
public class MyAdvice {
@Pointcut("execution(* com.sjtu.proxy..*ServiceImpl.*(..))")
public void pc() {}
//前置通知
//指定方法是前置通知,并制定切入点
@Before("MyAdvice.pc()")
public void before() {
System.out.println("这是前置通知!");
}
//后置通知
@AfterReturning("execution(* com.sjtu.proxy..*ServiceImpl.*(..))")
public void afterReturning() {
System.out.println("这是后置通知(如果出现异常不会调用)");
}
//环绕通知
@Around("execution(* com.sjtu.proxy..*ServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分!");
Object proceed = pjp.proceed();
System.out.println("这是环绕通知之后的部分!");
return proceed;
}
//异常通知
@AfterThrowing("execution(* com.sjtu.proxy..*ServiceImpl.*(..))")
public void afterException() {
System.out.println("异常出现了!");
}
//后置通知
@After("execution(* com.sjtu.proxy..*ServiceImpl.*(..))")
public void after() {
System.out.println("这是后置通知(出现异常也会调用)");
}
}
5.代码测试
//C3P0的连接池设定
//1.准备连接池
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
dataSource.setUser("root");
dataSource.setPassword("123456");
//2.创建JDBC模板对象
JdbcTemplate jTemplate=new JdbcTemplate();
jTemplate.setDataSource(dataSource);
//3.书写sql,并执行
String sql="insert into t_user values(null,'babdd')";
jTemplate.update(sql);
//使用JDBC模板实现增删改查
public class UserDaoIml implements UserDao{
//并且给jTemplate设置setter方法
private JdbcTemplate jTemplate;
@Override
public void save(User u) {
String sql="insert into t_user values(null,?)";
jTemplate.update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql="delete from t_user where id=?";
jTemplate.update(sql, id);
}
@Override
public void update(User u) {
String sql="update t_user set name=? where id=?";
jTemplate.update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql="select * from t_user where id=?";
return jTemplate.queryForObject(sql, new RowMapper() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}}, id);
}
@Override
public int getTotalCount() {
String sql="select count(*) from t_user";
Integer count = jTemplate.queryForObject(sql, Integer.class);
return count;
}
@Override
public List getAll() {
String sql="select * from t_user";
return jTemplate.query(sql,new RowMapper() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}});
}
public void setjTemplate(JdbcTemplate jTemplate) {
this.jTemplate = jTemplate;
}
}
4.spring配置
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///spring">property>
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
<property name="user" value="root">property>
<property name="password" value="123456">property>
bean>
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">property>
bean>
<bean name="userDao" class="com.sjtu.template.UserDaoIml">
<property name="jTemplate" ref="jdbcTemplate">property>
bean>
beans>
5.测试
package com.sjtu.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sjtu.bean.User;
import com.sjtu.template.UserDao;
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
//将名为userDao的对象注入ud变量中
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun1() {
User u=new User();
u.setName("Jerry");
ud.save(u);
}
@Test
public void fun2() {
ud.delete(1);
}
@Test
public void fun3() {
User u=new User();
u.setId(4);
u.setName("YYY");
ud.update(u);
}
@Test
public void fun4() {
System.out.println(ud.getById(4));
}
@Test
public void fun5() {
System.out.println(ud.getAll());
}
}
6.继承JDBCDaoSupport
package com.sjtu.template;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.sjtu.bean.User;
//使用JDBC模板实现增删改查
public class UserDaoIml extends JdbcDaoSupport implements UserDao{
@Override
public void save(User u) {
String sql="insert into t_user values(null,?)";
super.getJdbcTemplate().update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql="delete from t_user where id=?";
super.getJdbcTemplate().update(sql, id);
}
@Override
public void update(User u) {
String sql="update t_user set name=? where id=?";
super.getJdbcTemplate().update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql="select * from t_user where id=?";
return super.getJdbcTemplate().queryForObject(sql, new RowMapper() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}}, id);
}
@Override
public int getTotalCount() {
String sql="select count(*) from t_user";
Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return count;
}
@Override
public List getAll() {
String sql="select * from t_user";
return super.getJdbcTemplate().query(sql,new RowMapper() {
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
return user;
}});
}
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///spring">property>
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
<property name="user" value="root">property>
<property name="password" value="123456">property>
bean>
<bean name="userDao" class="com.sjtu.template.UserDaoIml">
<property name="dataSource" ref="dataSource">property>
bean>
beans>
7.改进:使用配置文件
db.properties 在src目录下
jdbc.jdbcUrl=dbc:mysql:///spring
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
"1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
"classpath:db.properties"/>
"dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
"jdbcUrl" value="${jdbc.jdbcUrl}">
"driverClass" value="${jdbc.driverClass}">
"user" value="${jdbc.user}">
"password" value="${jdbc.password}">
"userDao" class="com.sjtu.template.UserDaoIml">
"dataSource" ref="dataSource">
回顾知识
事务特性
事务并发问题
事务操作对象
PlatformTransactionManager
接口,里面有许多实现类:如JDBC–DataSourceTransactionManager,在spring中事务管理最为核心的对象:TransactionManagerpackage com.ali.dao;
public interface AccountDao {
//加钱
void increaseMoney(Integer id, Double money);
//减钱
void decreaseMoney(Integer id,Double money);
}
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void increaseMoney(Integer id, Double money) {
super.getJdbcTemplate().update("update t_account set money =money+? where id=?",money,id);
}
@Override
public void decreaseMoney(Integer id, Double money) {
// TODO Auto-generated method stub
super.getJdbcTemplate().update("update t_account set money =money-? where id=?",money,id);
}
}
package com.ali.service;
public interface AccountService {
void transfer(Integer from ,Integer to,Double money);
}
package com.ali.service;
import com.ali.dao.AccountDao;
public class AccountServiceImpl implements AccountService {
//注意此名字要与xml中的名字一致,否则无法注入
private AccountDao accountDao;
@Override
public void transfer(Integer from, Integer to, Double money) {
// TODO Auto-generated method stub
ad.decreaseMoney(from, money);
ad.increaseMoney(to, money);
}
//set方法注入,
public void setAccountDao(AccountDao aDao) {
this.accountDao = aDao;
}
}
"1.0" encoding="UTF-8"?>
"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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
"dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
"jdbcUrl" value="jdbc:mysql:///spring">
"driverClass" value="com.mysql.jdbc.Driver">
"user" value="root">
"password" value="123456">
"accountDao" class="com.ali.dao.AccountDaoImpl">
"dataSource" ref="dataSource">
"accountService" class="com.ali.service.AccountServiceImpl">
"accountDao" ref="accountDao">
6.事务管理:xml配置;注释配置
"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
"dataSource" ref="dataSource">
xml配置
此时,通知由SpringAop提供,代理对象account已经写好,还有织入(配置)就可以完成。
导包:4+2+2(aop+aspect)+aop联盟+weaving织入包
导入新的约束(tx)
配置通知
"1.0" encoding="UTF-8"?>
"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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
"classpath:db.properties" />
"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
"dataSource" ref="dataSource" >
"txAdvice" transaction-manager="transactionManager">
"transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
"get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
"find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
"execution(* com.ali.service.*ServiceImpl.*(..))" id="txPc"/>
"txAdvice" pointcut-ref="txPc"/>
"dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
"jdbcUrl" value="${jdbc.jdbcUrl}" >
"driverClass" value="${jdbc.driverClass}" >
"user" value="${jdbc.user}" >
"password" value="${jdbc.password}" >
"accountDao" class="com.ali.dao.AccountDaoImpl">
"dataSource" ref="dataSource">
"accountService" class="com.ali.service.AccountServiceImpl">
"accountDao" ref="accountDao">
代码测试
注解配置
导包
引入新的约束(tx)
开启注解管理事务
"1.0" encoding="UTF-8"?>
"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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
"classpath:db.properties" />
"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
"dataSource" ref="dataSource" >
"dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
"jdbcUrl" value="${jdbc.jdbcUrl}" >
"driverClass" value="${jdbc.driverClass}" >
"user" value="${jdbc.user}" >
"password" value="${jdbc.password}" >
"accountDao" class="com.ali.dao.AccountDaoImpl">
"dataSource" ref="dataSource">
"accountService" class="com.ali.service.AccountServiceImpl">
"accountDao" ref="accountDao">
注解(注意:注解在类上加可以,在方法上加也可以)
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(Integer from, Integer to, Double money) {
// TODO Auto-generated method stub
accountDao.decreaseMoney(from, money);
accountDao.increaseMoney(to, money);
}
//set方法注入
public void setAccountDao(AccountDao aDao) {
this.accountDao = aDao;
}
}
代码测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo2 {
@Autowired
private AccountService aService;
@Test
public void fun1() {
aService.transfer(1, 2, 100d);
}
}