美文网首页
Spring Aop配置文件使用

Spring Aop配置文件使用

作者: torres1 | 来源:发表于2019-12-17 11:41 被阅读0次

前言

本文介绍通过xml的方式来配置aop

1.编写目标类

@Data
public class Apple {
    private String name;
    private int price;
    public void grow() {
        System.out.println("Apple grow.......");
    }
}

2.编写通知

public class AppleAdvice {

    public void before(JoinPoint joinPoint) {
        System.out.println("advice before ...");
    }

    public void after(JoinPoint joinPoint) {
        System.out.println("advice after ...");
    }

    public void afterReturn(JoinPoint joinPoint) {
        System.out.println("advice after return ...");
    }

    public void afterThrow(JoinPoint joinPoint) {
        System.out.println("advice after throw ...");
    }

    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("around before_");
        point.proceed();
        System.out.println("around after_");
    }
}

3.配置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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--被切入对象-->
    <bean id="apple" class="com.cqliu.aop.Apple"/>
    <!--定义通知-->
    <bean id="appleAdvice" class="com.cqliu.aop.AppleAdvice"/>
    <aop:config>
        <!--定义切面-->
        <aop:aspect id="advisor" ref="appleAdvice">
            <!--定义切点-->
            <aop:pointcut id="pointcut" expression="execution(* com.cqliu.aop.Apple.*grow())"/>
            <!--定义通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturn" pointcut-ref="pointcut"/>
            <aop:after-throwing method="afterThrow" pointcut-ref="pointcut"/>
            <aop:around method="around" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

4.输出

public class SpringAopApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop-beans.xml");
        Apple apple = (Apple) applicationContext.getBean("apple");
        apple.grow();
    }
}
advice before ...
around before_
Apple grow.......
around after_
advice after return ...
advice after ...

5.切点表达式说明

  1. public void com.cqliu.aop.Apple.grow() //一个完整的方法路径

  2. void void com.cqliu.aop.Apple.grow()//省略默认的public

    • void com.cqliu.aop.Apple.grow() //返回类型不要求 用*取代
    • void com.cqliu.aop.Apple.grow().() //对象方法全设为目标方法 用取代
    • void com.cqliu.aop.Apple.grow().*(..) //方法的参数不作要求 用..表示
    • com.cqliu.aop.Apple.(..) //所有以Apple结尾类名 用*Apple表示
    • com.cqliu.aop..Apple.(..) //com.cqliu.aop包下的所有子包也包过在内加入.表示

相关文章

网友评论

      本文标题:Spring Aop配置文件使用

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