Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

版本5.0.0 报空指针异常,注册两次分页插件 #26

Closed
cookier opened this issue Mar 4, 2017 · 5 comments
Closed

版本5.0.0 报空指针异常,注册两次分页插件 #26

cookier opened this issue Mar 4, 2017 · 5 comments

Comments

@cookier
Copy link

cookier commented Mar 4, 2017

嗨,我遇到了一个问题,跟 #9 一样。
可我只配置了一次,PageInterceptor中intercept方法却被调用了2次,换成4.2的版本就正常了。
我的设置如下:

<property name="plugins">
            <array>
                <!--配置mybatis分页插件-->
                <bean class="com.github.pagehelper.PageInterceptor"/>
            </array>
        </property>

请问还有可能是哪里的问题呢?
@wolazy

@abel533
Copy link
Collaborator

abel533 commented Mar 4, 2017

正常配置的情况下,只有用 pagehelper-starter 1.0.0 版本才会出现。

单独的分页插件,只有配置多次才会出现。

@cookier
Copy link
Author

cookier commented Mar 5, 2017

经过断点调试发现,需要以下配置:

<bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>dialect=</value>
                    </property>
                </bean>

根据我的观察,PageIntercepor.setProperties方法里是设置dialect属性值的,如果在配置文件中没有调用到setProperties方法,则dialect为空,也没有默认值,所以导致执行插件的intercept方法时,调用dialect.afterAll()方法发生NPE。

@abel533
Copy link
Collaborator

abel533 commented Mar 5, 2017

XMLConfigBuilder 中:

private void pluginElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        String interceptor = child.getStringAttribute("interceptor");
        Properties properties = child.getChildrenAsProperties();
        Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
        interceptorInstance.setProperties(properties);
        configuration.addInterceptor(interceptorInstance);
      }
    }
  }

源码中,不管 properties 是否有值,都会执行 setProperties 方法,不是你说的问题。你可以看看这里。

@cookier
Copy link
Author

cookier commented Mar 6, 2017

生成XMLConfigBuilder的前提是有mybatis的配置文件,可我是用spring的属性配置方式。这样在SqlSessionFactoryBean.buildSqlSessionFactory的方法里由于configLocation属性值为空,不会生成xmlConfigBuilder对象,所以都不会执行到pluginElement方法。

@abel533
Copy link
Collaborator

abel533 commented Mar 6, 2017

非常感谢!!
确实有这个问题。
后面我会考虑在 public Object plugin(Object target) 方法中初始化插件。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants