根据(一)的内容,使用切入点以及切面完善存取钱的业务逻辑
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.运行效果