Spring AOP(二)aspect

参考链接:
https://blog.csdn.net/u011983531/article/details/70504281
https://blog.csdn.net/qq_41767337/article/details/89077073
结构
在这里插入图片描述
依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

applicationContext.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-->
    <bean id="myService" class="aop.MyServiceImpl" />
    <bean id="myService2" class="aop.MyService2" />

    <bean id="log" class="aop.Log" />


    <aop:config>

        <aop:aspect ref="log">
            <aop:pointcut id="pointcut" expression="execution(* aop.MyServiceImpl.*(..))" />
            <aop:before method="before" pointcut-ref="pointcut" />
            <aop:before method="before2" pointcut-ref="pointcut" />
            <aop:after method="after" pointcut-ref="pointcut" />
        </aop:aspect>

        <aop:aspect ref="log">
            <aop:pointcut id="pointcut2" expression="execution(* aop.MyService2.*(..))" />
            <aop:before method="before" pointcut-ref="pointcut2" />
            <aop:before method="before2" pointcut-ref="pointcut2" />
            <aop:after method="after" pointcut-ref="pointcut2" />
        </aop:aspect>
    </aop:config>

</beans>

代码
MyService

public interface MyService {
    void m1();
    String m2();
}

**MyServiceImpl **

public class MyServiceImpl implements MyService {

    public void m1() {
        System.out.println("执行方法m1");
    }

    public String m2() {
        System.out.println("执行方法m2:返回Hello World!");
        return "Hello World!";
    }
}

**MyService2 **

public class MyService2 {

    public void m1() {
        System.out.println("执行方法m1");
    }

    public String m2() {
        System.out.println("执行方法m2:返回Hello World!");
        return "Hello World!";
    }
}

Log

import org.aspectj.lang.JoinPoint;

public class Log {

    public void before(JoinPoint joinPoint) {
        System.out.println("前置通知:" + joinPoint.getTarget().getClass().getSimpleName()+ "的"+
                joinPoint.getSignature().getName()+ "方法开始执行");
    }
    public void before2(JoinPoint joinPoint) {
        System.out.println("前置通知2:" + joinPoint.getTarget().getClass().getSimpleName()+ "的"+
                joinPoint.getSignature().getName()+ "方法开始执行");
    }

    public void after() {
        System.out.println("后置通知");
    }

}

测试代码

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

public class AopTest {

    public static void main(String[] args) {
        System.out.println("加载xml文件");
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        MyService myServiceImpl = applicationContext.getBean("myService", MyService.class);
        MyService2 myService2 = applicationContext.getBean("myService2", MyService2.class);

        myServiceImpl.m1();
        System.out.println("---------");
        myServiceImpl.m2();
        System.out.println("---------");
        myService2.m1();
        System.out.println("---------");
        myService2.m2();

    }

}

结果输出

加载xml文件
前置通知:MyServiceImpl的m1方法开始执行
前置通知2:MyServiceImpl的m1方法开始执行
执行方法m1
后置通知
---------
前置通知:MyServiceImpl的m2方法开始执行
前置通知2:MyServiceImpl的m2方法开始执行
执行方法m2:返回Hello World!
后置通知
---------
前置通知:MyService2的m1方法开始执行
前置通知2:MyService2的m1方法开始执行
执行方法m1
后置通知
---------
前置通知:MyService2的m2方法开始执行
前置通知2:MyService2的m2方法开始执行
执行方法m2:返回Hello World!
后置通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值