1.Spring的AOP基于AspectJ的注解开发
目标类:
packagecom.albertyy.demo1; /** * * 项目名称:SpringDay03_AOP * 类名称:OrderDao * 类描述: * 创建人:yangyangyang * 创建时间:2018年12月7日 下午2:58:44 * 修改人:yangyangyang * 修改时间:2018年12月7日 下午2:58:44 * 修改备注: * @version * */ publicclass OrderDao{ publicvoid save(){ System.out.println("保存订单..."); } publicvoid update(){ System.out.println("修改订单..."); } public Stringdelete(){ System.out.println("删除订单..."); return"李君莫"; } publicvoid find(){ System.out.println("查询订单..."); // intd = 1/0; } }
在配置文件配置中配置目标类:
<!-- 配置目标类 --> <bean id="orderDao" class="com.albertyy.demo1.OrderDao"> </bean>
public class MyAspectAnno{ public void before(){ System.out.println("前置增强....."); } } <!-- 配置切面类--> <bean id="myAspect" class="com.albertyy.demo1.MyAspectAnno"> </bean>
l 在配置文件中打开注解的AOP开发
<!-- 在配置文件中开启注解的AOP的开发 --> <aop:aspectj-autoproxy/> l 在切面类上使用注解 /** * * 项目名称:SpringDay03_AOP * 类名称:MyAspectAnno * 类描述:注解的切面类 * 创建人:yangyangyang * 创建时间:2018年12月7日 下午3:00:55 * 修改人:yangyangyang * 修改时间:2018年12月7日 下午3:00:55 * 修改备注: * @version * */ @Aspect publicclassMyAspectAnno { @Before(value="execution(*com.albertyy.demo1.OrderDao.save(..))") publicvoid before(){ System.out.println("前置增强....."); } }
packagecom.albertyy.demo1; importjavax.annotation.Resource; import org.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * 项目名称:SpringDay03_AOP * 类名称:SpringDemo1 * 类描述:Spring的AOP的注解开发 * 创建人:yangyangyang * 创建时间:2018年12月7日 下午2:59:42 * 修改人:yangyangyang * 修改时间:2018年12月7日 下午2:59:42 * 修改备注: *@version * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringDemo1 { @Resource(name="orderDao") private OrderDao orderDao; @Test public void demo1(){ orderDao.save(); orderDao.update(); orderDao.delete(); orderDao.find(); } }
@Before(value="execution(*com.albertyy.demo1.OrderDao.save(..))") publicvoid before(){ System.out.println("前置增强....."); }
// 后置通知: @AfterReturning(value="execution(*com.albertyy.demo1.OrderDao.delete(..))",returning="result") publicvoidafterReturning(Object result){ System.out.println("后置增强....."+result); }
// 环绕通知: @Around(value="execution(*com.albertyy.demo1.OrderDao.update(..))") public Objectaround(ProceedingJoinPoint joinPoint) throwsThrowable{ System.out.println("环绕前增强....."); Objectobj = joinPoint.proceed(); System.out.println("环绕后增强....."); returnobj; }
// 异常抛出通知: @AfterThrowing(value="execution(*com.albertyy.demo1.OrderDao.find(..))",throwing="e") publicvoidafterThrowing(Throwable e){ System.out.println("异常抛出增强....."+e.getMessage()); }
// 最终通知 @After(value="execution(*com.albertyy.demo1.OrderDao.find(..))") publicvoid after(){ System.out.println("最终增强......."); }
packagecom.albertyy.demo1; importorg.aspectj.lang.ProceedingJoinPoint; importorg.aspectj.lang.annotation.After; importorg.aspectj.lang.annotation.AfterReturning; importorg.aspectj.lang.annotation.AfterThrowing; importorg.aspectj.lang.annotation.Around; importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.Before; importorg.aspectj.lang.annotation.Pointcut; /** * * 项目名称:SpringDay03_AOP * 类名称:MyAspectAnno * 类描述:注解的切面类 * 创建人:yangyangyang * 创建时间:2018年12月7日 下午3:00:55 * 修改人:yangyangyang * 修改时间:2018年12月7日 下午3:00:55 * 修改备注: * @version * */ @Aspect publicclassMyAspectAnno { @Before(value="MyAspectAnno.pointcut2()") publicvoid before(){ System.out.println("前置增强....."); } // 后置通知: @AfterReturning(value="MyAspectAnno.pointcut4()",returning="result") publicvoidafterReturning(Object result){ System.out.println("后置增强....."+result); } // 环绕通知: @Around(value="MyAspectAnno.pointcut3()") public Objectaround(ProceedingJoinPoint joinPoint) throwsThrowable{ System.out.println("环绕前增强....."); Objectobj = joinPoint.proceed(); System.out.println("环绕后增强....."); returnobj; } // 异常抛出通知: @AfterThrowing(value="MyAspectAnno.pointcut1()",throwing="e") publicvoidafterThrowing(Throwable e){ System.out.println("异常抛出增强....."+e.getMessage()); } // 最终通知 @After(value="MyAspectAnno.pointcut1()") publicvoid after(){ System.out.println("最终增强......."); } // 切入点注解: @Pointcut(value="execution(*com.albertyy.demo1.OrderDao.find(..))") privatevoidpointcut1(){} @Pointcut(value="execution(*com.albertyy.demo1.OrderDao.save(..))") privatevoidpointcut2(){} @Pointcut(value="execution(*com.albertyy.demo1.OrderDao.update(..))") privatevoidpointcut3(){} @Pointcut(value="execution(*com.albertyy.demo1.OrderDao.delete(..))") privatevoidpointcut4(){} }
Spring是EE开发的一站式的框架,有EE开发的每层的解决方案。Spring对持久层也提供了解决方案:ORM模块和JDBC的模板。
Spring提供了很多的模板用于简化开发:
l 创建项目,引入jar包
n 引入基本开发包:
n 数据库驱动
n Spring的JDBC模板的jar包
create database spring4_day03; use spring4_day03; create table account( id intprimary key auto_increment, namevarchar(20), moneydouble );
packagecom.albertyy.jdbc.demo1; importorg.junit.Test; importorg.springframework.jdbc.core.JdbcTemplate; importorg.springframework.jdbc.datasource.DriverManagerDataSource; /** * * 项目名称:SpringDay03_JDBC * 类名称:JdbcDemo1 * 类描述:JDBC模板的使用 * 创建人:yangyangyang * 创建时间:2018年12月7日 下午4:13:47 * 修改人:yangyangyang * 修改时间:2018年12月7日 下午4:13:47 * 修改备注: * @version * */ publicclass JdbcDemo1{ @Test // jdbc模板的使用类似于Dbutils. publicvoid demo1(){ // 创建连接池: DriverManagerDataSourcedataSource = newDriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring4_day03"); dataSource.setUsername("root"); dataSource.setPassword("123456"); // 创建jdbc模板 JdbcTemplatejdbcTemplate = newJdbcTemplate(); jdbcTemplate.setDataSource(dataSource); jdbcTemplate.update("insertinto account values (null,?,?)", "君陌",10000d); } }
<?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"> <!-- 配置Spring的内置的连接池======================== --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///spring4_day03"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- 配置Spring的JDBC的模板=========================--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
l 引入spring_aop的jar包
packagecom.albertyy.jdbc.demo1; importjavax.annotation.Resource; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.jdbc.core.JdbcTemplate; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * 项目名称:SpringDay03_JDBC * 类名称:JdbcDemo2 * 类描述: 将连接池和模板交给Spring管理 * 创建人:yangyangyang * 创建时间:2018年12月7日 下午4:35:27 * 修改人:yangyangyang * 修改时间:2018年12月7日 下午4:35:27 * 修改备注: * @version * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") publicclass JdbcDemo2{ @Resource(name="jdbcTemplate") privateJdbcTemplate jdbcTemplate; @Test // 保存操作 publicvoid demo1(){ jdbcTemplate.update("insertinto account values (null,?,?)", "张小花儿",10000d); } }
l 引入jar包
l 配置DBCP连接池
<!-- 配置DBCP连接池=============================== --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///spring4_day03"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- 配置Spring的JDBC的模板=========================--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean>
l 引入c3p0连接池jar包
l 配置c3p0连接池
<!-- 配置C3P0连接池=============================== --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///spring4_day03"/> <property name="user" value="root"/> <property name="password" value="123456"/> </bean> <!-- 配置Spring的JDBC的模板=========================--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean>
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///spring4_day03 jdbc.username=root jdbc.password=123456
l 第一种:
<!-- 第一种方式通过一个bean标签引入的(很少用) --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean>
l 第二种:
<!-- 第二种方式通过context标签引入的 --> <context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池=============================== --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
@Test // 保存操作 publicvoid demo1(){ jdbcTemplate.update("insertinto account values (null,?,?)", "十三先生",10000d); }
@Test // 保存操作 publicvoid demo1(){ jdbcTemplate.update("insertinto account values (null,?,?)", "陈皮皮",10000d); }
@Test // 修改操作 publicvoid demo2(){ jdbcTemplate.update("updateaccount set name = ? ,money = ? where id = ?", "叶红鱼",2000d,1); }
@Test // 删除操作 publicvoid demo3(){ jdbcTemplate.update("deletefrom account where id = ?", 6); }
l 查询某个属性
@Test // 查询操作: publicvoid demo4(){ Stringname = jdbcTemplate.queryForObject("selectname from account where id = ?", String.class, 5); System.out.println(name); } @Test // 统计查询 publicvoid demo5(){ Longcount = jdbcTemplate.queryForObject("selectcount(*) from account", Long.class); System.out.println(count); }
l 查询返回对象或集合
@Test // 封装到一个对象中 publicvoid demo6(){ Accountaccount = jdbcTemplate.queryForObject("select* from account where id = ?", newMyRowMapper(), 5); System.out.println(account); } @Test // 查询多条记录 publicvoid demo7(){ List<Account>list = jdbcTemplate.query("select* from account", newMyRowMapper()); for (Account account : list) { System.out.println(account); } }
l 数据封装
classMyRowMapper implementsRowMapper<Account>{ @Override public AccountmapRow(ResultSet rs, introwNum) throwsSQLException { Accountaccount = newAccount(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getDouble("money")); returnaccount; } }
l 事务:逻辑上的一组操作,组成这组操作的各个单元,要么全都成功,要么全都失败。
l 原子性:事务不可分割.
l 一致性:事务执行前后数据完整性保持一致。
l 隔离性:一个事务的执行不应该受到其他事务的干扰。
l 持久性:一旦事务结束,数据就持久化到数据库。
l 读问题
n 脏读 :一个事务读到另一个事务未提交的数据
n 不可重复读 :一个事务读到另一个事务已经提交的update的数据,导致一个事务中多次查询结果不一致
n 虚读、幻读 :一个事务读到另一个事务已经提交的insert的数据,导致一个事务中多次查询结果不一致。
l 写问题
n 丢失更新
l 设置事务的隔离级别
n Read uncommitted :未提交读,任何读问题都解决不了。
n Read committed :已提交读,解决脏读,但是不可重复读和虚读有可能发生。
n Repeatable read :重复读,解决脏读和不可重复读,但是虚读有可能发生。
n Serializable :解决所有读问题。
l 平台事务管理器:接口,是Spring用于管理事务的真正的对象。
n DataSourceTransactionManager :底层使用JDBC管理事务
n HibernateTransactionManager :底层使用Hibernate管理事务
l 事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为、是否只读
l 事务状态:用于记录在事务管理过程中,事务的状态的对象。
Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。
l Spring中提供了七种事务的传播行为:
n 保证多个操作在同一个事务中
u
PROPAGATION_REQUIRED
:默认值,如果A中有事务,使用A中的事务,如果A没有,创建一个新的事务,将操作包含进来。
u
PROPAGATION_SUPPORTS
:支持事务,如果A中有事务,使用A中的事务。如果A没有事务,不使用事务。
u PROPAGATION_MANDATORY
:如果A中有事务,使用A中的事务。如果A没有事务,抛出异常。
n 保证多个操作不在同一个事务中
u
PROPAGATION_REQUIRES_NEW
:如果A中有事务,将A的事务挂起(暂停),创建新事务,只包含自身操作。如果A中没有事务,创建一个新事务,包含自身操作。
u
PROPAGATION_NOT_SUPPORTED
:如果A中有事务,将A的事务挂起。不使用事务管理。
u
PROPAGATION_NEVER
:如果A中有事务,报异常。
n 嵌套式事务
u
PROPAGATION_NESTED
:嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。
l 创建Service的接口和实现类
packagecom.albertyy.tx.demo1; /** * 转账的业务层的实现类 * @author yxy * */ publicclassAccountServiceImpl implementsAccountService { // 注入DAO: privateAccountDao accountDao; publicvoidsetAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override /** * from:转出账号 to:转入账号 money:转账金额 */ publicvoid transfer(final String from, final String to, final Double money) { accountDao.outMoney(from, money); intd = 1 / 0; accountDao.inMoney(to, money); } }
l 创建DAO的接口和实现类
packagecom.albertyy.tx.demo1; importorg.springframework.jdbc.core.support.JdbcDaoSupport; /** * 转账的DAO的实现类 * @author yxy * */ publicclass AccountDaoImpl extendsJdbcDaoSupport implementsAccountDao { @Override publicvoidoutMoney(String from, Double money) { this.getJdbcTemplate().update("updateaccount set money = money - ? where name = ?", money,from); } @Override publicvoidinMoney(String to, Double money) { this.getJdbcTemplate().update("updateaccount set money = money + ? where name = ?", money ,to); } }
l 配置Service和DAO:交给Spring管理
<!-- 配置Service --> <bean id="accountService" class="com.albertyy.tx.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> <!-- 配置DAO --> <bean id="accountDao" class="com.albertyy.tx.demo1.AccountDaoImpl"> </bean>
l 在DAO中编写扣钱和加钱方法:
n 配置连接池和JDBC的模板
<!-- 配置连接池和JDBC的模板 --> <!-- 第二种方式通过context标签引入的 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池===============================--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
n 在DAO注入Jdbc的模板:
<!-- 配置DAO=================--> <bean id="accountDao" class="com.albertyy.tx.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean>
l 测试
packagecom.albertyy.tx.demo1; importjavax.annotation.Resource; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 测试转账的环境 * @author yxy * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx.xml") publicclassSpringDemo1 { @Resource(name="accountService") privateAccountService accountService; @Test publicvoid demo1(){ accountService.transfer("张丫丫", "莫山山", 100d); } }
<!-- 配置平台事务管理器=============================--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
l 配置事务的管理的模板类
<!-- 配置事务管理的模板 --> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean>
<!-- 配置Service============= --> <bean id="accountService" class="com.albertyy.tx.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> <!-- 注入事务管理的模板 --> <property name="trsactionTemplate" ref="transactionTemplate"></property> </bean>
@Override /** * from:转出账号 to:转入账号 money:转账金额 */ publicvoid transfer(final String from, final String to, final Double money) { trsactionTemplate.execute(newTransactionCallbackWithoutResult() { @Override protectedvoiddoInTransactionWithoutResult(TransactionStatus transactionStatus) { accountDao.outMoney(from, money); intd = 1 / 0; accountDao.inMoney(to, money); } }); }
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx.xml") publicclassSpringDemo1 { @Resource(name="accountService") privateAccountService accountService; @Test publicvoid demo1(){ accountService.transfer("张丫丫", "莫山山", 100d); } }
l 第一步:引入aop的开发包
l 第二步:恢复没有配置事务的转账环境
l 第三步:配置事务管理器
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
l 第四步:配置增强
<!-- 配置事务的增强=============================== --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 事务管理的规则 --> <!--<tx:method name="save*" propagation="REQUIRED"isolation="DEFAULT"/> <tx:methodname="update*" propagation="REQUIRED"/> <tx:methodname="delete*" propagation="REQUIRED"/> <tx:methodname="find*" read-only="true"/> --> <tx:method name="*" propagation="REQUIRED"read-only="false"/> </tx:attributes> </tx:advice>
l 第五步:AOP的配置
<!-- aop的配置 --> <aop:config> <aop:pointcut expression="execution(*com.albertyy.tx.demo2.AccountServiceImpl.*(..))" id="pointcut1"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> </aop:config>
l 测试
packagecom.albertyy.tx.demo2; importjavax.annotation.Resource; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 测试转账的环境 * @author yxy * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx2.xml") publicclassSpringDemo1 { @Resource(name="accountService") privateAccountService accountService; @Test publicvoid demo1(){ accountService.transfer("张丫丫", "莫山山", 100d); } }
l 第一步:引入aop的开发包
l 第二步:恢复没有配置事务的转账环境
l 第三步:配置事务管理器
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
l 第四步:开启注解事务
<!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/>
l 第五步:在业务层添加注解
@Transactional publicclassAccountServiceImpl implementsAccountService { l 第六步:测试 packagecom.albertyy.tx.demo3; importjavax.annotation.Resource; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 测试转账的环境 * @author yxy * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:tx3.xml") publicclassSpringDemo1 { @Resource(name="accountService") privateAccountService accountService; @Test publicvoid demo1(){ accountService.transfer("陈皮皮", "莫山山", 100d); } }
源码:链接: https://pan.baidu.com/s/1dTOhQI8DOy5lKWAiCxnfzg提取码: swbh
本文分享自微信公众号 - AlbertYang(AlbertYang666),作者:AlbertYang
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2018-12-07
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句