Day23 Spring AOP与spring事务管理

Spring AOP

AOP术语

1、 目标类target:就是我们需要增强的那个类 LaoZong.class
2、 代理类proxy:就是自定义的代理的对象 $Proxy0.class
3、 连接点joinPoint:程序执行的某个特定位置,Spring仅支持方法的连接点
eat(),sleep(),run()
4、 切入点pointCut:就是在目标类中实际增强的方法
eat()
5、 织入weave:就是将代理类中需要增强的方法放入到目标类中去执行的过程
将原方法与其他类的方法一起调用
6、 引介Introduction:引介是一种特殊的增强,它为类添加一些属性和方法(课程不使用)
7、 通知advice:将代理对象中的方法应用到目标类的过程中产生的结果。
8、 切面aspect:所有的切入点和代理对象的方法组成在一起 构成了切面

SpringAOP搭建

  • 导入依赖
  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
    </dependencies>
  • 必须要有一个接口
public interface IUser {
    void work();
}
  • 接口实现类
public class UserImpl implements IUser {
    @Override
    public void work() {
        System.out.println("接口实现类");
    }
}
  • 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"
       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">
    <bean id="userImpl" class="com.dsf.service.impl.UserImpl"/>
<!--    创建Advice对象-->
    <bean id="advice" class="com.dsf.service.Advice"/>
<!--    配置他们的增强关系 生成一个代理类-->
    <aop:config >
        <aop:pointcut id="work" expression="execution(public void com.dsf.service.impl.UserImpl.work())"/>
        <!-- 我想在work方法执行 之后调用一下writeLog-->
        <aop:aspect ref="advice">
             <aop:after method="writeLog"  pointcut-ref="work"/>
        </aop:aspect>
    </aop:config>
</beans>
  • Advice.java
public class Advice {
    public void writeLog(){
        System.out.println("另一个类");
    }
}
  • 测试类
public class UserImplTest {
    @Autowired
    IUser iUser;
    @Test
    public void work() {
        System.out.println(iUser);
        iUser.work();;
    }
}

Spring AOP切面编程

  • 表达式
    execution([修饰符] 返回值类型 包.类.方法(参数列表) );
1:完全写法
execution(public   void  com.dsf.service.UserImpl.work(int) );
2:-修饰符省略
execution( void  com.dsf.service.UserImpl.work(int) );
3:简化2-返回值类型写通配符
execution(*  com.dsf.service.UserImpl.work(int) );
4:简化3-包名写通配符
execution(*  *.*.*.UserImpl.work(int) );
5:简化4-包名通配符简化
execution(*  *..UserImpl.work(int) );
6:简化5-类名通配符
execution(*  *..*.work(int) );
6:简化6-参数写通配符
execution(*  *..*.work(..) );

作用
方法增强
使用* 表示任意的内容
使用 (..)可以表示包与子包下面的类
使用..可以写在方法(..)表示任意参数

<aop:config >
        <aop:pointcut id="work" expression="execution(* com.dsf.service..*.*(..))"/>
        <aop:aspect ref="advice">
            <aop:after method="writeLog"  pointcut-ref="work"/>
        </aop:aspect>
    </aop:config>
  • 增强方式
 try{
            //开始执行before
            before();
            point.proceed();//环绕通知
            afterReturn();//执行正常
        }catch (Throwable e){
            afterThrow();//异常
        }finally {
            after();//最后释放资源
        }
    }

before();和after();方法不管程序有无问题都执行–>相当于在try()中和finally()中
afterReturn();和around();环绕通知只有程序正常运行时执行–>相当于在try()中
afterThrow();在程序有异常时执行–>相当于在catch()中

案例
创建一个类

public class Advice {
    public void before(){
        System.out.println("before-------");
    }
    public void after(){
        System.out.println("after-------");
    }
    public void afterReturn(){
        System.out.println("afterReturn-------");
    }
    public void afterThrow(){
        System.out.println("afterThrow-------");
    }
}

applicationContext.xml配置

 <aop:config >
        <aop:pointcut id="all" expression="execution( * com.wzx.service..*.*(..))"/>
        <aop:aspect ref="advice">
            <aop:before method="before"  pointcut-ref="all"/>
            <aop:after-returning method="afterReturn"  pointcut-ref="all"/>
            <aop:after-throwing method="afterThrow"  pointcut-ref="all"/>
            <aop:after method="after"  pointcut-ref="all"/>
        </aop:aspect>
    </aop:config>

环绕通知
创建一个类

public void arround(ProceedingJoinPoint point ){//参目标类中的任意方法
        try{
            //执行before
            before();
            point.proceed(); - arround
            //执行正常返回
            afterReturn();
        }catch (Throwable e){
            //补救方法
            afterThrow();
        }finally {
            //释放资源
            after();
        }
    }

applicationContext.xml

 <aop:config >
        <aop:pointcut id="all" expression="execution( * com.dsf.service..*.*(..))"/>
        <aop:aspect ref="advice">
            <aop:around method="arround"  pointcut-ref="all"/>
        </aop:aspect>
    </aop:config>
  • 注解
    创建一个类
@Component
@Aspect
public class Advice {
    @Around("execution( * com.dsf.service..*.*(..))")
    public void arround(ProceedingJoinPoint point ){//参目标类中的任意方法
        try{
            //执行before
            before();
            System.out.println("arround-------");
            point.proceed();
            //执行正常返回
            afterReturn();
        }catch (Throwable e){
            //补救方法
            afterThrow();
        }finally {
            //释放资源的方法
            after();
        }
    }
}

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: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.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">
    <!--包扫描创建对象 扫描包与它下面的子包中的所有类-->
    <context:component-scan base-package="com.dsf.service"/>
    <!--开启自动AOP代理-->
    <aop:aspectj-autoproxy/>
</beans>

Spring 事务管理

事务就是保证一组操作在进行过程中要么同时成功,要么同时失败
事务管理可以保持多并发情况下数据一致、如果出现错误的操作可以进行改正
事务的特性

原子性: 一个事务是最小的执行单元,不可以分割
一致性: 事务指定的前后,数据要保持一致
持久性: 事务一旦提交或回滚,在数据库中持久的改变
隔离性: 隔离性用来保证多个用户来操作数据库的同一份数据,不会受到相关干扰的特性

事务的隔离级别
在这里插入图片描述
脏读:一个事务读到了另一个事务的还没有提交数据.(update)
不可重复读: 一个事务中多次读到的数据不一致.一个事务读到了另一个事务修改后的数据.(update)
幻读:一个事务读到了insert的数据.(insert)

传播行为:
在这里插入图片描述

案例–>事务xml方式
导入依赖

<dependencies>
        <!-- jdbc-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- ioc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <!-- jdbctemplate-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <!-- test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
       <!-- aop-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.2</version>
        </dependency>
    </dependencies>

编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestAccountServiceImpl {
    @Autowired
    IAccountService service ;
    @Test
    public void test01(){
        service.translate("jack","rose",500.00);
    }
}

applicationContext.xml

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="adviceId" transaction-manager="transactionManager">
        <tx:attributes>
            <!--
                该增强中的规则:
                name代表筛选那些方法收到事务的增强
                isolation : 隔离级别 REPEATABLE_READ 可重复读
                propagation: 传播行为
                read-only: 是否为只读事务
                timeout: 事务是否超时
            -->
            <tx:method name="translate" isolation="REPEATABLE_READ"  propagation="REQUIRED" read-only="false" timeout="-1" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="translate" expression="execution(* com.dsf.service.AccountServiceImpl.translate(..))"/>
        <aop:advisor advice-ref="adviceId" pointcut-ref="translate"/>
    </aop:config>
</beans>

创建接口类

public interface IAccountDao {
    @Deprecated
    void update(String account, double v);
    void translateOut(String account, double v);
    void translateIn(String account, double v);
}

创建接口实现类AccountDaoImpl

@Repository
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public void translateOut(String account, double v) {
        jdbcTemplate.update("update account set money=money-? where `name`=?",v,account);
    }
    @Override
    public void translateIn(String account, double v) {
        jdbcTemplate.update("update account set money=money+? where `name`=?",v,account);
    }
}

再创建一个IAccountService类

public interface IAccountService {
    void translate(String from, String to, double money);
}

再创建一个IAccountService实现类AccountServiceImpl

@Service
public class AccountServiceImpl implements IAccountService{
    @Autowired
    IAccountDao dao ;
    @Override
    public void translate(String from, String to, double money) {
        dao.translateOut(from,money);
        //System.out.println(1/0); //模拟断电
        dao.translateIn(to,money);
        System.out.println("--转账--");
    }
}

事务注解方式–>@Transactional()
给AccountServiceImpl 类上添加一个@Transactional注解即可
再给xml重新添加 注解驱动 (支持注解方式的事务)

applicationContext.xml

 <!--1.要在xml中创建spring的平台事务管理器(DataSourceTransactionManager)  注入dataSource-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--2.配置注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

AccountServiceImpl.java

@Service
public class AccountServiceImpl implements IAccountService{
    @Autowired
    IAccountDao dao ;
    @Transactional(isolation = Isolation.REPEATABLE_READ ,propagation = Propagation.REQUIRED ,timeout = -1)
    @Override
    public void translate(String from, String to, double money) {
        dao.translateOut(from,money);
        //System.out.println(1/0); //模拟断电
        dao.translateIn(to,money);
        System.out.println("--转账--");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值