#AOP的四种常用通知#前置通知、后置通知、异常通知、 最终通知#aop:before、aop:after-returning、aop:after-throwing、aop:after@FDDLC

项目结构:

 

pom.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>P026_FourKindsOfAdvice</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
    </dependencies>

</project>

 

bean.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 https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean class="cn.liuxingchang.Pointcut" id="pointcut"></bean>
    <bean class="cn.liuxingchang.Advice" id="advice"></bean>

    <aop:config>
        <aop:aspect ref="advice" id="advice">
            <!--前置通知-->
            <aop:before method="before" pointcut="execution(public void cn.liuxingchang.Pointcut.save())"></aop:before>
            <!--后置通知-->
            <aop:after-returning method="afterReturning" pointcut="execution(public void cn.liuxingchang.Pointcut.save())"></aop:after-returning>
            <!--异常通知-->
            <aop:after-throwing method="afterThrowing" pointcut="execution(public void cn.liuxingchang.Pointcut.save())"></aop:after-throwing>
            <!--最终通知-->
            <aop:after method="after" pointcut="execution(public void cn.liuxingchang.Pointcut.save())"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

 

Pointcut.java的内容:

package cn.liuxingchang;

public class Pointcut {

    public void save() {
        System.out.println("save");
        //下面的语句用于测试异常通知
        //int i = 1/0;
    }
}

 

Advice.java的内容:

package cn.liuxingchang;

public class Advice {
    public void before() {
        System.out.println("before");
    }

    public void afterReturning() {
        System.out.println("afterReturning");
    }

    public void afterThrowing() {
        System.out.println("afterThrowing");
    }

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

 

Main.java的内容:

package cn.liuxingchang;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Pointcut pointcut = context.getBean("pointcut", Pointcut.class);
        pointcut.save();
    }
}

 

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页