26.Spring-AOP(切面编程)

本文介绍了Spring的面向切面编程(AOP)概念,包括AOP的JDK和cglib动态代理实现,详细讲解了XML配置AOP的各个部分,如切点表达式的写法、不同类型的通知,以及如何抽取切点表达式。同时,文章还探讨了注解配置AOP的方法,包括注解通知的类型和切点表达式的使用。

目录

一、Spring-AOP。

(1)AOP的简介。 

(2)AOP的底层实现-动态代理。 

(2.1)JDK的动态代理。

 (2.2)cglib的动态代理。

 (3)AOP的相关概念。 

(4)xml配置——AOP的快速入门。

(5) xml配置AOP详解。

(5.1)切点表达式的写法。 

(5.2)通知的类型。

(5.3)切点表达式的抽取。

(5.4)知识要点。

(6)注解配置——AOP的快速入门。

(7)注解配置AOP详解。

(7.1)注解通知的类型。

​(7.2)切点表达式的抽取。

(7.3) 知识要点。 


一、Spring-AOP。

(1)AOP的简介。 

(2)AOP的底层实现-动态代理。 

(2.1)JDK的动态代理。

 (2.2)cglib的动态代理。

 (3)AOP的相关概念。 

(4)xml配置——AOP的快速入门。

<?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">
    <!--1、目标对象-->
    <bean id="target" class="aop.Target"/>
    <!--2、切面对象-->
    <bean id="myAspect" class="aop.MyAspect"></bean>
    <!--3、配置织入:告诉spring框架,哪些方法需要进行哪些增强(前置,后置,等等...)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--切面:切点+通知,注意下面的这组</aop:before>标签要在同一行才行,不然会报错-->
            <aop:before method="before" pointcut="execution(public void aop.Target.save())"></aop:before>
            <aop:around method="around" pointcut="execution(* aop..*.*(..))"/>
            <aop:before method="before" pointcut="execution(* aop.*.*(..))"></aop:before>
            <aop:after-returning method="afterReturning" pointcut="execution(* aop.*.*(..))"/>
            <aop:after-throwing method="afterThrowing" pointcut="execution(* aop..*.*(..))"></aop:after-throwing>
            <aop:after method="after" pointcut="execution(* *..*.*(..))"/>

        <!--抽取切点表达式-->
            <aop:pointcut id="myPointcut" expression="execution(* aop.*.*(..))"/>
            <aop:around method="around" pointcut-ref="myPointcut"/>
            <aop:after method="after" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

(5) xml配置AOP详解。

(5.1)切点表达式的写法。 

(5.2)通知的类型。

(5.3)切点表达式的抽取。

(5.4)知识要点。

(6)注解配置——AOP的快速入门。

(7)注解配置AOP详解。

(7.1)注解通知的类型。

(7.2)切点表达式的抽取。

@Component("myAspect")
@Aspect//标注当前MyAspect是一个切面类
public class MyAspect {  
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjo) throws Throwable {
        System.out.println("环绕前增强......");
        Object proceed = pjo.proceed();//切点方法
        System.out.println("环绕后增强......");
        return proceed;
    }  
    //@After("execution(* *..*.*(..))")
    @After("MyAspect.pointcut()")
    public void after(){
        System.out.println("最终增强......");
    }

    //定义一个切点表达式
    @Pointcut("execution(* *..*.*(..))")
    public void pointcut(){ }//这个只是借助这个方法写注解,即便把这个改成字段也行...

}

 @Around("pointcut()") 与@Around("类名.pointcut()")都可以。

(7.3) 知识要点。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值