Spring AOP

Spring AOP

面向切面编程
面向切面编程(Aspect Oriented Programming, AOP)是软件编程思想发展到一定阶段的产物,是对面向对象编程(Object Oriented Programming, OOP)的有益补充。AOP 一般适用于具有横切逻辑的场合,如访问控制、事物管理、性能监测等。
简单来说就是在不改变原程序的基础上为代码段增强新的功能,对代码段进行增强处理。给编程人员的感觉就是在原有代码乃至原业务流程都不修改的情况下,直接在业务流程中切入新代码,增强新功能,这就是所谓的面型切面编程。
面向切面编程的基本概念:
(1)切面(Aspect):一个模块化的横切逻辑(或称横切关注点),可能会横切多个对象。
(2)连接点(Join Point):程序执行中的某个具体的执行点。
(3)增强处理(Advice):切面在某个特定连接点上执行的代码逻辑。
(4)切入点(Pointcut):对连接点的特征进行描述,可以使用正则表达式。增强处理和一个切入点表达式相关联,并在与这个切入点匹配的某个连接点上运行。
(5)目标对象(Target object):被一个或多个切面增强的对象。
(6)AOP 代理(AOP proxy):由AOP框架所创建的对象,实现执行增强处理方法等功能。
(7)织入(Weaving):将增强处理连接到应用程序中的类型或对象上的过程。
(8)增强处理类型:在原对象的fun()方法之前插入的增强处理为前置增强,该方法正常执行完以后插入的增强处理为后置增强,此外还有环绕增强、异常抛出增强。最终增强的类型。

Target类:

package aop;

public class Target {
    public String show(String name){
        System.out.println("这是目标方法");
        return "hello"+name;
    }
}

AdviceTarget 类:

package aop;

import org.aspectj.lang.JoinPoint;

//增强类
public class AdviceTarget {
    //前置增强
    public void before(JoinPoint jp) {
        System.out.println("参数:" + jp.getArgs() + ",方法名:" + jp);
    }

    public void after(JoinPoint jp) {
        System.out.println("这是后置增强...");
    }

    public void afterReturn(JoinPoint jp, String vaule) {
        System.out.println("后置带返回值" + vaule);
    }

    public void afterException(JoinPoint jp, Exception ex) throws Exception {
        System.out.println("后置带异常" + ex);
        if (1 / 0 == 1) {
            throw new Exception("除数不为0");
        }
    }
}

spring_aop.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="target" class="aop.Target"/>
    <bean id="AdviceTarget" class="aop.AdviceTarget"/>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(public String show(String))"/>
        <aop:aspect ref="AdviceTarget">
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

测试类

public void testaop(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring_aop.xml");
        Target target = (Target)context.getBean("target");
        try{
            target.show("mary");
        }catch (Exception e){
            System.out.println("除数不能为0");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值