Home > Java > javaTutorial > mybatis interceptor

mybatis interceptor

(*-*)浩
Release: 2019-09-03 16:41:13
forward
2063 people have browsed it

One of the functions of the interceptor is that we can intercept the calls of certain methods. We can choose to add some logic before and after the execution of these intercepted methods, or discard these intercepted methods and execute our own logic. .

mybatis interceptor

For example, for the Executor of mybatis, there are several implementations: BatchExecutor, ReuseExecutor, SimpleExecutor and CachingExecutor. When the query methods of these Executor interfaces cannot When our requirements are met, we can build an interceptor to implement our own query method; interceptors are generally implemented dynamically using aop.

Interceptor principle

For mybatis, we can define our own interceptor through the interceptor interface. Interceptor interface definition:

1

2

3

4

5

6

7

package org.apache.ibatis.plugin;

import java.util.Properties;

public interface Interceptor {

    Object intercept(Invocation invocation) throws Throwable;

    Object plugin(Object target);

    void setProperties(Properties properties);

}

Copy after login

The plugin method is mainly used to encapsulate the target object. Through this method, we can decide whether to intercept and then decide what kind of target object to return.

The intercept method is the method that is executed when intercepting. setProperties is mainly used to specify properties in the configuration file. This method will be executed when Configuration initializes the current Interceptor. There is a plugin class in mybatis, which includes the static method wrap. Through this method, it can be determined whether the object to be returned is the target. Objects or agents.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

package org.apache.ibatis.plugin;

  

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Map;

import java.util.Set;

import org.apache.ibatis.reflection.ExceptionUtil;

  

public class Plugin implements InvocationHandler {

  

    private Object target;

    private Interceptor interceptor;

    private Map<Class<?>, Set<Method>> signatureMap;

  

    private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {

        this.target = target;

        this.interceptor = interceptor;

        this.signatureMap = signatureMap;

    }

  

    public static Object wrap(Object target, Interceptor interceptor) {

        //解析获取需要拦截的类以及方法{*}

        Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);

        Class<?> type = target.getClass();

        //解析type是否存在需要拦截的接口{*}

        Class<?>[] interfaces = getAllInterfaces(type, signatureMap);

        //决定返回的对象是否为代理{*}

        if (interfaces.length > 0) {

            return Proxy.newProxyInstance(

                type.getClassLoader(),

                interfaces,

                new Plugin(target, interceptor, signatureMap));

        }

        //返回原目标对象

        return target;

    }

  

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        try {

            Set<Method> methods = signatureMap.get(method.getDeclaringClass());

            //如果当前执行的方法是定义的需要拦截的方法,则把目标对象,要拦截的方法以及参数封装为一个Invocation对象传递给拦截器方法intercept;

            //Invocation中定义了定义了一个proceed方法,其逻辑就是调用当前方法,所以如果在intercept中需要继续调用当前方法的话可以调用invocation的procced方法;

            if (methods != null && methods.contains(method)) {

                return interceptor.intercept(new Invocation(target, method, args));

            }

            return method.invoke(target, args);

        } catch (Exception e) {

            throw ExceptionUtil.unwrapThrowable(e);

        }

    }

  

    //根据注解解析需要拦截的方法

    //两个重要的注解:@Intercepts以及其值其值@Signature(一个数组)

    //@Intercepts用于表明当前的对象是一个Interceptor

    //@Signature则表明要拦截的接口、方法以及对应的参数类型。

    private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {

        Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);

        if (interceptsAnnotation == null) { // issue #251

            throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());

        }

        Signature[] sigs = interceptsAnnotation.value();

        Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();

        for (Signature sig : sigs) {

            Set<Method> methods = signatureMap.get(sig.type());

            if (methods == null) {

                methods = new HashSet<Method>();

                signatureMap.put(sig.type(), methods);

            }

            try {

                Method method = sig.type().getMethod(sig.method(), sig.args());

                methods.add(method);

            } catch (NoSuchMethodException e) {

                throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);

            }

        }

        return signatureMap;

    }

  

    private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>>  signatureMap) {

        Set<Class<?>> interfaces = new HashSet<Class<?>>();

        while (type != null) {

            for (Class<?> c : type.getInterfaces()) {

                if (signatureMap.containsKey(c)) {

                    interfaces.add(c);

                }

            }

            type = type.getSuperclass();

        }

        return interfaces.toArray(new Class<?>[interfaces.size()]);

    }

}

Copy after login

Interceptor instance

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package com.mybatis.interceptor;

  

import java.sql.Connection;

import java.util.Properties;

  

import org.apache.ibatis.executor.Executor;

import org.apache.ibatis.executor.statement.StatementHandler;

import org.apache.ibatis.mapping.MappedStatement;

import org.apache.ibatis.plugin.Interceptor;

import org.apache.ibatis.plugin.Intercepts;

import org.apache.ibatis.plugin.Invocation;

import org.apache.ibatis.plugin.Plugin;

import org.apache.ibatis.plugin.Signature;

import org.apache.ibatis.session.ResultHandler;

import org.apache.ibatis.session.RowBounds;

  

@Intercepts( {

@Signature(method = "query", type = Executor.class, args = {

MappedStatement.class, Object.class, RowBounds.class,

ResultHandler.class })})

public class TestInterceptor implements Interceptor {

    public Object intercept(Invocation invocation) throws Throwable {

        Object result = invocation.proceed();

        return result;

    }

  

    public Object plugin(Object target) {

        return Plugin.wrap(target, this);

    }

  

    public void setProperties(Properties properties) {

        String p = properties.getProperty("property");

    }

}

Copy after login

First mark this as an Interceptor with @Intercepts, and design the interception point through @Signatrue: the parameter type in the interceptor Executor interface is MappedStatement, The query method of Object, RowBounds and ResultHandler; the intercept method calls the proceed method of invocation to make the current method call normally.

Registration of interceptors

Registration of interceptors is carried out through the plugin element under the plugins element in the Mybatis configuration file. Mybatis is defined in the registration When using an interceptor, all properties under the corresponding interceptor will first be injected through the setProperties method of the Interceptor. Such as:

1

2

3

4

5

<plugins>

    <plugin interceptor="com.mybatis.interceptor.TestInterceptor">

        <property name="property" value="拦截器配置"/>

    </plugin>

</plugins>

Copy after login

The above is the detailed content of mybatis interceptor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template