Spring 面向切面编程(aop)

概念:

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方
式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个
热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑
的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

练习代码:

AdminCheck:

BankDaoImpl:
public class BankDaoImpl implements IBankDao {


    @Override
    public void saveMoney() {
//        adminCheck.check();
//        transactionManager.beginTransaction();
        System.out.println("正在存钱");
//        logInfo.WriterLog();
//        transactionManager.commitTransaction();
    }

    @Override
    public void sendMoney() {
        System.out.println("正在取钱");
    }

    @Override
    public void loan() {
        System.out.println("正在贷款操作");
    }

    @Override
    public void manageMoneyMatters() {
        System.out.println("正在理财操作");
    }

    @Override
    public void transfer_Accouts() {
        System.out.println("正在转账");
    }

}

LogInfo:

 

TransactionManager:
public class TransactionManager {
    public void beginTransaction(){
        System.out.println("事务的开始");
    }
    public void commitTransaction(){
        System.out.println("事务的提交");
    }
    public void doincepter(ProceedingJoinPoint point){
        try {
            beginTransaction();
            point.proceed();
            commitTransaction();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

接口

IBankDao:

 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: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-4.3.xsd
">

    <bean id="adminCheck" class="ty.dao.impl.AdminCheck"></bean>
    <bean id="transactionManager" class="ty.dao.impl.TransactionManager"></bean>
    <bean id="logInfo" class="ty.dao.impl.LogInfo"></bean>
    <bean id="bankDao" class="ty.dao.impl.BankDaoImpl">
<!--        <property name="adminCheck" ref="adminCheck"></property>-->
<!--        <property name="transactionManager" ref="transactionManager"></property>-->
<!--        <property name="logInfo" ref="logInfo"></property>-->
    </bean>

    <aop:config>
<!--        访问业务逻辑的时候切入-->
        <aop:pointcut id="servicepoint" expression="execution(* ty.dao.impl.*.*(..))"/>
<!--        id="servicepoint"-->
        <aop:aspect  ref="adminCheck">
            <aop:before method="check" pointcut-ref="servicepoint"/>
        </aop:aspect>
        <aop:aspect  ref="transactionManager">
            <aop:around method="doincepter" pointcut-ref="servicepoint"/>
        </aop:aspect>
        <aop:aspect ref="logInfo">
            <aop:after method="WriterLog" pointcut-ref="servicepoint" />
        </aop:aspect>
    </aop:config>
</beans>

测试类:

public class Test1 {
    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        IBankDao bd=ac.getBean("bankDao",IBankDao.class);
        bd.sendMoney();//对应你调用增强类的任意方法
        System.out.println("------");
        bd.transfer_Accouts();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值