spring AOP @Around @Before @After 区别

此段小代码演示了spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。

1、创建接口HelloWorld

import java.util.List;

public interface HelloWorld {
    List doPrint(String name);
}

 

2、创建接口HelloWorld的实现类HelloWorldImpl

import com.google.common.collect.Lists;
import com.longteng.lesson2.dao.HelloWorld;

import java.util.List;

public class HelloWorldImpl implements HelloWorld {
    @Override
    public List doPrint(String name) {
        System.out.println("HelloWorldImpl::doPrint()"+name);
        List list = Lists.newArrayList();
        list.add(name);
        return list;
    }
}

 

3、创建切面类和切面方法

import com.google.common.collect.Lists;
import org.aspectj.lang.ProceedingJoinPoint;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TimeHandler {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public void startTime(){
        System.out.println("currentTime= "+simpleDateFormat.format(new Date()));
    }
    public void endTime(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("EndTime= "+simpleDateFormat.format(new Date()));
    }
    public List around(ProceedingJoinPoint proceedingJoinPoint){
        List list = Lists.newArrayList();
        System.out.println("before around==========");
        Object[] args = proceedingJoinPoint.getArgs();
        args[0]="我是被修改的参数值";
        try {
            Object result = proceedingJoinPoint.proceed(args);
            result="返回值被修改了。。。";
            list.add(result);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("after around==========");
        return list;
    }
}

 

4、编辑aop.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 http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="timeHandler" class="com.longteng.lesson2.service.TimeHandler"></bean>
    <bean id="helloWorldImpl" class="com.longteng.lesson2.dao.Impl.HelloWorldImpl"></bean>

    <aop:config>
        <aop:aspect id="handler" ref="timeHandler">
            <aop:pointcut id="time" expression="execution(* com.longteng.lesson2.dao.Impl.HelloWorldImpl.*(..))"></aop:pointcut>
            <aop:before method="startTime" pointcut-ref="time"></aop:before>
            <aop:after method="endTime" pointcut-ref="time"></aop:after>
            <aop:around method="around" pointcut-ref="time"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

 

5、创建测试类

import com.longteng.lesson2.dao.Impl.HelloWorldImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class AopTest {
    ApplicationContext context;
    @BeforeClass
    public void initContext(){
        context = new ClassPathXmlApplicationContext("aop.xml");

    }
    @Test
    public void test(){
        HelloWorld helloWorld =  context.getBean("helloWorldImpl",HelloWorld.class);
        System.out.println( helloWorld.doPrint("zhou"));
        System.out.println("-------------------------------------------");
    }
}

 

6、获取测试结果

11:11:20.218 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldImpl'
11:11:20.224 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
currentTime= 2018-12-24 11:11:20
11:11:20.224 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
before around==========
HelloWorldImpl::doPrint()我是被修改的参数值
after around==========
11:11:20.228 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
EndTime= 2018-12-24 11:11:23
[返回值被修改了。。。]
-------------------------------------------

 

posted @ 2018-12-24 11:17  小周lucky  阅读(9773)  评论(0编辑  收藏  举报