spring 用注解实现一个简单的AOP的Demo

IoC相关的基本内容告一段落,本次介绍Spring的第二个特性,AOP,面向切面编程,术语听起来比较不容易理解,没关系,一切尽在实例中,让我们看一个简单的实例,就能明白。

首先看下整体结构

这里面有几个类,

第一 ,pom.xml是里面需要引用jar的,

第二:定义一个接口   Hellointerface  实现接口为:UserServiceImpl

第三:添加一个 root-context.xml

第四:添加Aop类  TimeMonitor

第五:springaopapplication 测试类 

第一步 创建一个接口  这个就当着正常的类,跟aop思想没有任何关系

创建接口  HelloInterface

package springaop.aopservice;

public interface HelloInterface {
    void sayHello();
}

第二步:创建实现类  UserServiceImpl

package springaop.aopservice.impl;

import org.springframework.stereotype.Service;
import springaop.aopservice.HelloInterface;

@Service
public class UserServiceImpl  implements HelloInterface {
    public void sayHello() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("userHello");
    }
}

第三步:创建  root-context.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-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <context:component-scan base-package="springaop"></context:component-scan>

</beans>

第四步:在pom.xml里面添加 

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

第五步:添加aop的类

这里需要了解一下aop的5种注解

 @after   通知方法会在目标方法返回或抛异常后调用

@afterreturning  通知方法会在返回后调用

@AfterThrowing  通知方法会在抛异常后调用

@Around     通知方法会将目标方法封装起来   也就是 在方法前后都执行。用 Object re = pjp.proceed();区分前后

@Before  通知方法会在目标方法调用之前执行

package springaop.aopservice.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class TimeMonitor {

    @Pointcut("execution(* springaop.aopservice.impl.UserServiceImpl.sayHello(..))")
    public  void  performance(){}

    @Around("performance()")
    public void monitorAround(ProceedingJoinPoint pjp) throws Throwable {

        System.out.println("method start time:" + System.currentTimeMillis());

        Object re = pjp.proceed();

        System.out.println("method end time:" + System.currentTimeMillis());

    }


    @Before("performance()")
    public  void  tekeSeats(){

        System.out.println("我来测试一下,看看能不能先执行method end time:" + System.currentTimeMillis());
    }

    @AfterReturning("performance()")
    public  void  afterSeats(){

        System.out.println("我来测试一下,看看能不能后执行method end time:" + System.currentTimeMillis());
    }

}

第六步:

测试

package springaop.springaop;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import springaop.aopservice.HelloInterface;

@SpringBootTest
class SpringaopApplicationTests {

    ///通过XMl的方式实现 aop
    @Test
    void contextLoads() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml");
        HelloInterface userService = context.getBean(HelloInterface.class);
        userService.sayHello();
        context.close();
    }


}

猜你喜欢

转载自blog.csdn.net/xulong5000/article/details/107788341