设为首页 加入收藏

TOP

12、Spring之基于xml的AOP(二)
2023-08-26 21:10:55 】 浏览:81
Tags:Spring xml AOP
p://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 对指定的package进行扫描,将使用组件注解的类的对象(本示例是目标对象和切面对象),交给spring的ioc容器来管理 --> <context:component-scan base-package="org.rain.spring.aop.xml"></context:component-scan> </beans>

12.2、前置通知的使用

12.2.1、基本示例

12.2.1.1、定义前置通知的功能

image

    public void beforeMethod(){
        System.out.println("LoggerAspect,前置通知");
    }

12.2.1.2、配置前置通知到切面

image

    <aop:config>
        <!--
            aop:aspect标签:将ioc容器中的bean对象设置为切面,效果等同于@Aspect注解
                ref属性:引用IOC容器中(要设置为切面的)bean的id
        -->
        <aop:aspect ref="loggerAspect">
            <!--
                aop:before标签:将方法设置为该切面的前置通知(方法),效果等同于@Before注解
                    method属性:指定(要设置为前置通知的)方法的名称
                    pointcut属性:设置该前置通知的切入点表达式
            -->
            <aop:before method="beforeMethod" pointcut="execution(* org.rain.spring.aop.xml.CalculatorImpl.*(..))"></aop:before>
        </aop:aspect>
    </aop:config>

12.2.1.3、测试使用效果

image

由控制台日志可知,切面类的前置通知(方法),通过切入点表达式,作用到了目标方法的连接点上

package org.rain.spring.aop.test;

import org.junit.Test;
import org.rain.spring.aop.xml.Calculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author liaojy
 * @date 2023/8/18 - 19:07
 */
public class AOPTest {

    @Test
    public void testAOPByXML(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-aopByxml.xml");
        // 注意:这里不能直接获取目标对象来使用;因为使用了AOP之后,IOC容器中就只有对应目标对象的代理对象;
        // 如果强行获取目标对象,则报错:NoSuchBeanDefinitionException
        //Calculator calculator = ioc.getBean(CalculatorImpl.class);

        // 虽然不知道代理对象的类名,但可以通过代理对象和目标对象共同实现的接口类型来从ioc容器中获取代理对象
        Calculator calculator = ioc.getBean(Calculator.class);

        // 只能通过代理对象来访问目标对象中的方法
        calculator.div(1,1);

    }
}

12.2.2、高级示例

12.2.2.1、改进前置通知的功能

image

该示例中(前置)通知方法引入了连接点参数,通过连接点参数,可以动态获取(切入点表达式)对应的目标方法的名称和参数列表

    // joinPoint参数:可以获取(通过切入点表达式定位出的)连接点的相关信息
    public void beforeMethod(JoinPoint joinPoint){
        // 获取连接点所对应目标方法的名称
        String methodName = joinPoint.getSignature().getName();
        // 获取连接点所对应目标方法的参数列表
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect-->前置通知,方法名:"+methodName+",参数:"+ Arrays.toString(args));
    }

12.2.2.2、测试使用效果

image

12.3、切入点表达式的复用

切入点表达的详细语法,请参考11.3.2节

12.3.1、配置公共的切入点表达式

image

        <!--
            aop:pointcut标签:用于声明公共的切入点表达式,效果等同于@Pointcut注解
                id属性:设置该公共切入点表达式的唯一标识
                expression属性:设置切入点表达式
        -->
        <aop:pointcut id="pointcutOne" expression="execution(* org.rain.spring.aop.xml.CalculatorImpl.*(..))"/>

12.3.2、使用公共的切入点表达式

image

<!--
    aop:before标签:将方法设置为该切面的前置通知(方法),效果等同于@Before注解
        method属性:指定(要设置为前置通知的)方法的名称
        pointcut属性:设置该前置通知的切入点表达式
        pointcut-ref属性:通过id引用对应的公共切入点表达式
-->
<!--<aop:before method="beforeMethod" pointcut="execution(* org.rain.spring.aop.xml.CalculatorImpl.*(..))"></aop:before>-->
<aop:before method="beforeMethod" pointcut-ref="pointcutOne"></aop:before>

12.4、其他通知的使用

12.4.1、后置通知

12.4.1.1、定义后置通知的功能

image

    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Docker下elasticsearch8部署、扩.. 下一篇面试官:String.intern() 有什么..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目