SpringAOP

作者: _FireFly_ | 来源:发表于2020-11-26 09:22 被阅读0次
/**
 * 这是一个切面对象
 */
public class Aspect1 {

    //切面对象中的分方法本身叫连接点
    //方法什么时候(最终目标之前 之后 。。)执行方式 建议
    public void beforeMethod(){
        System.out.println("预备 开始 喊~~~");
    }

    public void afterReturningMethod(){
        System.out.println("请坐");
    }

    public void afterThrowingMethod(){
        System.out.println("出现异常啦");
    }

    public void afterMethod(){
        System.out.println("最终我很开心");
    }

    public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {//特别像是以前的Filter  放行  底层责任链实现方式
        System.out.println("环绕建议前部分");
        //中间等待目标执行完
        pjp.proceed();//chain.doFilter();
        System.out.println("环绕建议后部分");
    }
}

TestController

/**
 * 这是一个Controller
 * 可以理解为是之前的Servlet(以后发送请求访问的那个目标对象)
 */
public class TestController {

    public void test(){
        System.out.println("阿拓老师很帅!!!");
    }
}

TestMain

public class TestMain {

    public static void main(String[] args) {
        //调用controller中的那个test方法
        BeanFactory factory = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //1.需要一个controller对象---->IOC管理对象
        TestController controller = (TestController)factory.getBean("controller");
        //2.对象.调用就开业啦
        controller.test();//目标方法

        /*
                try{
                    前置
                    环绕前
                    目标方法(环绕的是目标的前后)
                    环绕后
                    后置
                }catch(){
                    异常
                }finally{
                    最终
                }
         */
    }
}

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        ">

    <bean id="controller" class="controller.TestController"></bean>
    <bean id="aspect1" class="aspect.Aspect1"></bean>
    <bean id="aspect2" class="aspect.Aspect2"></bean>


    <!--给切面对象做一个说明-->
    <aop:config>
        <!--需要说明哪一个 哪一些对象是切面对象-->
        <aop:pointcut id="mycut" expression="execution(* controller.TestController.test())"></aop:pointcut>
        <aop:aspect id="as1" ref="aspect1">
            <!--说明切点 目标对象中的方法-->
            <!--expression 包 类 方法-->
            <!--切面对象做的建议方式(连接点的执行 连接点是切面中的方法)-->
            <aop:before method="beforeMethod" pointcut-ref="mycut"></aop:before>
            <aop:around method="aroundMethod" pointcut-ref="mycut"></aop:around>
            <aop:after-returning method="afterReturningMethod" pointcut-ref="mycut"></aop:after-returning>
            <aop:after-throwing method="afterThrowingMethod" pointcut-ref="mycut"></aop:after-throwing>
            <aop:after method="afterMethod" pointcut-ref="mycut"></aop:after>
        </aop:aspect>
        <!--第二个切面对象-->
        <aop:aspect id="as2" ref="aspect2">
            <aop:pointcut id="mycut" expression="execution(* controller.TestController.test())"></aop:pointcut>
            <aop:before method="beforeMethod" pointcut-ref="mycut"></aop:before>
            <aop:after-returning method="afterReturningMethod" pointcut-ref="mycut"></aop:after-returning>
        </aop:aspect>
    </aop:config>
</beans>

相关文章

  • spring框架 AOP

    10、 代理模式 为什么要学习代理模式?因为这就是SpringAOP的底层!【SpringAOP 和 Spring...

  • spring源码解析-基于注解的SpringAOP源码解析(二)

    在Spring源码解析之基于注解的SpringAOP源码解析(一)中,我们搭建了SpringAOP源码分析的环境,...

  • 六、AOP实现自动的系统日志功能

    一、本课目标 掌握SpringAOP的配置 二、使用SpringAOP实现日志输出 在下面的这个示例中,通过Spr...

  • SpringAOP

    SpringAOP-PPT SpringAOP视频 面向切面编程(AOP)通过提供另外一种思考程序结构的途经来弥补...

  • springAOP

    springAOP切面拦截参数进行校验。

  • Spring AOP源码分析

    前言 通过之前的俩篇文章,我们大体上已经知道如何使用SpringAOP了,同时也了解到了SpringAOP底层使用...

  • Spring AOP 一

    上一篇讲了jdk动态代理,下面我们来说说SpringAOP。SpringAOP是基于动态代理的,它对动态代理又做了...

  • SpringAOP

    大家好,我是IT修真院北京分院第31期的学员,一枚正直纯洁善良的JAVA程序员。今天给大家分享一下,修真院...

  • springAop

    springAop:面向切面的编程 应用场景:权限控制、事物管理、日志打印等等,就是在不同的方法中重复用到相同的代...

  • SpringAOP

    一. AOP定义 AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置运行的编程方式。 二. AOP流程 ...

网友评论

      本文标题:SpringAOP

      本文链接:https://www.haomeiwen.com/subject/dwmqiktx.html