设为首页 加入收藏

TOP

接口方法上的注解无法被 @Aspect 声明的切面拦截的原因分析(三)
2018-05-21 15:48:41 】 浏览:1683
Tags:接口 方法 注解 无法 @Aspect 声明 拦截 原因分析
ewProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); }

可以看到底层是通过JDK动态代理Proxy生成的,InvocationHandler是MapperProxy类。

清楚原理之后,我们对上面的实例做下改造,把Mybatis的引用简化。

@Configuration
public class DemoConfiguraion {
    
    @Bean
    public FactoryBean<DemoMapper> getDemoMapper() {
        return new FactoryBean<DemoMapper>() {
            @Override
            public DemoMapper getObject() throws Exception {
                InvocationHandler invocationHandler = (proxy, method, args) -> {
                    System.out.println("调用动态代理方法" + method.getName());
                    return Collections.singletonList(new HashMap<String, Object>());
                };
                return (DemoMapper) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {DemoMapper.class}, invocationHandler);
            }
            @Override
            public Class<?> getObjectType() {
                return DemoMapper.class;
            }
            @Override
            public boolean isSingleton() {
                return true;
            }
        };
    }
}

上面的代码可达到与Mapper同样的效果,大家可以本地随便玩哈。

首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用 Maven 实现一个 protobuf 的 J.. 下一篇直播一次问题排查过程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目