Spring学习——0216Aop

什么是AOP

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

AOP的专业名词
Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
Target(目标对象):织入 Advice 的目标对象.。
Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程

需要导入的依赖

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.2</version>
</dependency>

模拟AOP,首先新建一个接口

public interface Bank {
    public void saveMoney();
    public void withdrawMoney();
    public void theLoan();
    public void financial();
}

新建三个类,分别作为前置增强,围绕增强,后置增强

public class TheLogin {
    public void theLogin(){
        System.out.println("正在登录");
    }
}

public class TheTransaction {
    public void startTheTransaction(){
        System.out.println("开始事务");
    }
    public void endTheTransaction(){
        System.out.println("提交事务");
    }
    public void getTheTransaction(ProceedingJoinPoint point){
        try {
            startTheTransaction();
            point.proceed();
            endTheTransaction();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

public class TheLog {
    public void steatTheLog(){
        System.out.println("开始制作日志");
    }
}

实现这个接口(里面的三个属性分别是上面的三个类)

public class BankImpl implements Bank {
    public TheLog theLog;
    public TheLogin theLogin;
    public TheTransaction theTransaction;

    public TheLog getTheLog() {
        return theLog;
    }

    public void setTheLog(TheLog theLog) {
        this.theLog = theLog;
    }

    public TheLogin getTheLogin() {
        return theLogin;
    }

    public void setTheLogin(TheLogin theLogin) {
        this.theLogin = theLogin;
    }

    public TheTransaction getTheTransaction() {
        return theTransaction;
    }

    public void setTheTransaction(TheTransaction theTransaction) {
        this.theTransaction = theTransaction;
    }

    @Override
    public void saveMoney() {
        System.out.println("存钱");
    }

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

    @Override
    public void theLoan() {
        System.out.println("贷款");
    }

    @Override
    public void financial() {
        System.out.println("理财");
    }
}

XML中进行控制,首先给三个类和接口实现类属性注入

<bean id="theLog" class="impl.TheLog"></bean>
    <bean id="theLogin" class="impl.TheLogin"></bean>
    <bean id="theTransaction" class="impl.TheTransaction"></bean>
    <bean id="bankImpl" class="impl.BankImpl">
        <property name="theLog" ref="theLog"></property>
        <property name="theLogin" ref="theLogin"></property>
        <property name="theTransaction" ref="theTransaction"></property>
    </bean>

开始增强,依次前置,围绕,后置(需要上面生成东西,打完AOP:config会自己生成)

 <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* impl.*.*(..))"/>
        <aop:aspect ref="theLogin">
            <aop:before method="theLogin" pointcut-ref="pointcut1"></aop:before>
        </aop:aspect>
        <aop:aspect ref="theTransaction">
            <aop:around method="getTheTransaction" pointcut-ref="pointcut1"></aop:around>
        </aop:aspect>
        <aop:aspect ref="theLog">
            <aop:after method="steatTheLog" pointcut-ref="pointcut1"></aop:after>
        </aop:aspect>
    </aop:config>

测试类(记得用接口类转化)

public class Test {
    public static void main(String[] args) {
        test01();
    }
    public static void test01(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        Bank bankImpl= (Bank) applicationContext.getBean("bankImpl");//此处通过applicationContext.xml文件中的bean-id创建对象
        bankImpl.saveMoney();
    }
}

结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值