pagehelper-spring-boot icon indicating copy to clipboard operation
pagehelper-spring-boot copied to clipboard

addPageInterceptor执行两次,导致分页出错

Open is00hcw opened this issue 8 years ago • 10 comments

如题, 分页拦截执行两次,报空指针错误

is00hcw avatar Jan 11 '17 08:01 is00hcw

你哪儿为什么会执行两次?能断点调试看看原因吗?

abel533 avatar Jan 11 '17 12:01 abel533

我这里也是执行两次,第一次成功,第二次空指针

lgc0313 avatar Jan 12 '17 15:01 lgc0313

PageHelper执行clearPage,清空了数据, 后面再次调用getLocalPage就会报空指针错误了. 原因是用@PostConstruct标注的方法执行了两次导致. 暂时用下面的方法解决

@Configuration
@EnableConfigurationProperties(PageHelperAutoConfiguration.PageHelperProperties.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class PageHelperAutoConfiguration implements InitializingBean {

    private final SqlSessionFactory sqlSessionFactory;

    @Autowired
    private PageHelperProperties pageHelperProperties;

    public PageHelperAutoConfiguration(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;

    }

//    @PostConstruct
    public void addPageInterceptor() {
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.putAll(pageHelperProperties.getPagehelper());
        interceptor.setProperties(properties);
        sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        addPageInterceptor();
    }


    @ConfigurationProperties
    public static class PageHelperProperties{
        private Map<String, String> pagehelper = new LinkedHashMap<String, String>();

        public Map<String, String> getPagehelper() {
            return pagehelper;
        }

        public void setPagehelper(Map<String, String> pagehelper) {
            this.pagehelper = pagehelper;
        }
    }
}

is00hcw avatar Jan 16 '17 12:01 is00hcw

@is00hcw 为什么会两次呢?能确定原因吗?

abel533 avatar Jan 16 '17 13:01 abel533

@PostConstruct标注的方法被调用两次. 可以搜索一下类似的情况 http://blog.csdn.net/lxb_champagne/article/details/17614099

is00hcw avatar Jan 17 '17 02:01 is00hcw

建议查看以下spring源代码, 猜测是因为在PageHelperAutoConfiguration同时标注了@Configuration和@ConfigurationProperties, 导致@PostConstruct方法被执行两次. 而我上面贴的代码正是把这两个分开在不同的类上.

  • org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration#afterSingletonsInstantiated

  • org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#onApplicationEvent

  • org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#rebind()

  • org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)

  • org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization

is00hcw avatar Jan 17 '17 04:01 is00hcw

@is00hcw @Crazydiver 新发布了 1.1.0-beta ,你们试试吧。

abel533 avatar Jan 17 '17 15:01 abel533

pagehelper-spring-boot 发布了 1.1.0-beta 版本,试试这个版本。

2017-01-17 12:22 GMT+08:00 is00hcw [email protected]:

建议查看以下spring源代码, 猜测是因为在PageHelperAutoConfiguration同时标注了@Configuration https://github.com/Configuration和@ConfigurationProperties, 导致@PostConstruct方法被执行两次. 而我上面贴的代码正是把这两个分开在不同的类上.

org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinde rAutoConfiguration#afterSingletonsInstantiated

org.springframework.cloud.context.properties. ConfigurationPropertiesRebinder#onApplicationEvent

org.springframework.cloud.context.properties. ConfigurationPropertiesRebinder#rebind()

org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support. RootBeanDefinition)

org.springframework.beans.factory.annotation. InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pagehelper/pagehelper-spring-boot/issues/1#issuecomment-273019237, or mute the thread https://github.com/notifications/unsubscribe-auth/ABtHljpW-uMmi0-2mHbshKoPxkxnLJU2ks5rTEH9gaJpZM4LgTPT .

abel533 avatar Jan 17 '17 15:01 abel533

OK. 分页执行正常,

is00hcw avatar Jan 19 '17 15:01 is00hcw

@is00hcw 那就应该是你猜的这个原因,只是复现可能需要一定的条件。不用那种方式就好了。 最近会发布1.1.0版本,和 beta 版本没区别。

abel533 avatar Jan 20 '17 15:01 abel533