SpringAop业务逻辑简单讲解(存取钱案例 二)

7 篇文章 0 订阅
6 篇文章 0 订阅

根据(一)的内容,使用切入点以及切面完善存取钱的业务逻辑

1.BankDao接口(增加了一个业务)

package com.xiao.dao;

public interface BankDao {
//    存钱
    public void saveMoney();
//    取钱
    public void withdrawMoney();

//    转帐
    public void transfer();
}

2.实现接口(简化了一些代码)

package com.xiao.dao.daoImp;

import com.xiao.dao.BankDao;

public class BankDaoImp implements BankDao {

    @Override
    public void saveMoney() {
        System.out.println("存钱的业务逻辑");
    }

    @Override
    public void withdrawMoney() {
        System.out.println("取钱的业务逻辑");
    }

    @Override
    public void transfer() {
        System.out.println("转账的业务逻辑");
    }
}

3.事务(在事务这处打上断点,需要导入两个jar包---aspectjrt.jar/aspectjweaver.jar)

package com.xiao.dao.daoImp;

import org.aspectj.lang.ProceedingJoinPoint;

public class Transmaction {

    public void beginTransmaction(){
        System.out.println("开始事物");
    }

    public void closeTransmaction(){
        System.out.println("结束事物");
    }

    public void dointcepter(ProceedingJoinPoint point) throws Throwable {
        beginTransmaction();
        point.proceed();
        closeTransmaction();
    }
}

4.在事务中,由’point.proceed()‘指定业务逻辑,之后配置spring.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<bean id="adminCheck" class="com.xiao.dao.daoImp.AdminCheck"></bean>
	<bean id="transmaction" class="com.xiao.dao.daoImp.Transmaction"></bean>

    <bean id="bankDao" class="com.xiao.dao.daoImp.BankDaoImp">
	<!--<property name="adminCheck" ref="adminCheck"></property>-->
	<!--<property name="transmaction" ref="transmaction"></property>-->
	</bean>

	<aop:config>
		<!--切入点,什么时候,执行什么切入进来-->
		<aop:pointcut id="savepoint" expression="execution(* com.xiao.dao.daoImp.*.*(..))"></aop:pointcut>

		<!--切面-用来做事情-执行业务逻辑前-要做或者执行什么事情-->
		<aop:aspect id="adminincepter" ref="adminCheck">
			<aop:before method="check" pointcut-ref="savepoint"></aop:before>
		</aop:aspect>

		<aop:aspect id="transmactionincepter" ref="transmaction">
			<aop:around method="dointcepter" pointcut-ref="savepoint"></aop:around>
		</aop:aspect>
	</aop:config>



</beans>

5.业务逻辑完成就可以测试了

package com.xiao.dao.test;

import com.xiao.dao.BankDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        test02();
    }

    private static void test02(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring01.xml");
        BankDao bankDao = ac.getBean("bankDao", BankDao.class);
        bankDao.transfer();
    }
}

6.运行效果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值