spring中的核心内容就是aop(面向切面编程),姑且不谈spring中其他框架,
但就谈spring本身,spring属于业务层的框架,最主要的核心内容就是实现各层解耦,和事务管理:但是aop如何如何实现事务管理呢,代这里列举的是经典的转账例子,代码如下:
这里不再贴接口字体实现类代码:
dao层实现类:
package com.leige.dao.impl; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.leige.dao.UserDao; public class UserDaiImpl extends JdbcDaoSupport implements UserDao { //执行修改操作 @Override public void update(String name, Double money) { try { String sql="update t_user set money=money+? where name=?;"; super.getJdbcTemplate().update(sql,money,name); } catch (Exception e) { // TODO: handle exception System.out.println(e); } } }service层实现:
package com.leige.service.impl; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import com.leige.dao.UserDao; import com.leige.service.UserService; public class UserServiceImpl implements UserService{ UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Transactional(isolation=Isolation.DEFAULT) @Override//如果使用xml配置,这事务注解不用写 public void transfer(String from, String to, Double money) { // TODO Auto-generated method stub userDao.update(from, -money); /* int i=5; i=i/0;*/ userDao.update(to, money); } }
package com.leige.domain; public class User { private Integer id; private String name; private Double money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } }注解和xml配置实现:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <context:annotation-config/> <!-- 配置数据库连接池 --> <bean id="dataSourse" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"/> <property name="password" value=""/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="acquireIncrement" value="5"/> <property name="maxPoolSize" value="50"/> <property name="initialPoolSize" value="5"/> </bean> <!-- 配置业务层的注入 --> <bean id="userService" class="com.leige.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> <!-- 配置dao层的注入 --> <bean id="userDao" class="com.leige.dao.impl.UserDaiImpl"> <!-- 配置数据库连接池的注入 --> <property name="dataSource" ref="dataSourse"/> </bean> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSourse"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <!-- 配置事务详情,上一条是使用注解实现的事务管理,不管是使用注解还是xml事务管理器都是必须要配置的 <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> 设置要被管理的方法 <tx:method> 确定事务详情配置 name : 确定方法名称 transfer 确定名称 add* add开头 * 任意 propagation 传播行为 isolation 隔离级别 read-only="false" 是否只读 rollback-for="" 指定异常回滚(-) no-rollback-for="" 指定异常提交(+) 经典应用:开发中规定 <tx:method name="add*"/> <tx:method name="update*"/> <tx:method name="delete*"/> <tx:method name="find*" read-only="true"/> <tx:method name="transfer" read-only="false" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.leige.service.impl.*.*(..))" id="myPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/> </aop:config> --> </beans>
首先在web.xml中加入spring的监听器以及所需要的参数:
<!-- 通过 servletContext 初始化参数设置xml位置,否则默认是WEB-INF/applicationContext.xml中读取配置文件 记住参数的名称,可在源码中查看 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <!-- 配置spring的监听器,这样可以是服务器已启动就加载spring的配置文件在这里即是beans.xml --> <description>spring 中加载配置文件的监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>servlet中获取spring的容器:
package com.leige.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.leige.service.UserService; public class UserServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext=request.getServletContext(); //从Servlet中获取spring容器: //使用WebApplicationContextUtils中的方法获取ApplicationContext,需要传入一个ServletContext ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(servletContext); //得到容器之后就可以完成service层的操作 UserService userService=(UserService) context.getBean("userService"); userService.transfer("leige", "leige2", 100.0);//转账业务 request.getRequestDispatcher("/index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
注解实现需要注意的是,需要将事务交给spring管理:
将xml中配置事务管理的部分删除,添加一个:
<tx:annotation-driven transaction-manager="txManager"/>
即:可以添加在类上也可以添加在方法上,别忘记配置注解扫描
@Transactional(isolation=Isolation.DEFAULT) @Override public void transfer(String from, String to, Double money) { // TODO Auto-generated method stub userDao.update(from, -money); /* int i=5; i=i/0;*/ userDao.update(to, money); }