spring AOP @Around @Before @After 區別

此段小代碼演示了spring aop中@Around @Before @After三個註解的區別@Before是在所攔截方法執行以前執行一段邏輯。@After 是在所攔截方法執行以後執行一段邏輯。@Around是能夠同時在所攔截方法的先後執行一段邏輯。java

一、建立接口HelloWorldspring

import java.util.List;

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

 

二、建立接口HelloWorld的實現類HelloWorldImplexpress

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;
    }
}

 

三、建立切面類和切面方法less

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;
    }
}

 

四、編輯aop.xml配置文件ide

<?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>

 

五、建立測試類測試

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("-------------------------------------------");
    }
}

 

六、獲取測試結果google

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 [返回值被修改了。。。] -------------------------------------------
相關文章
相關標籤/搜索