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

PageHelper在SpringBoot的@PostConstruct 中不生效 #38

Closed
White-Duke opened this issue Apr 9, 2018 · 4 comments
Closed

PageHelper在SpringBoot的@PostConstruct 中不生效 #38

White-Duke opened this issue Apr 9, 2018 · 4 comments

Comments

@White-Duke
Copy link

White-Duke commented Apr 9, 2018

依赖版本 pagehelper-spring-boot-starter 1.2.4
从debug结果来看,Bean的PostConstruct执行的时候,Pagehelper的autoconfigure还没有初始化。
和git上面另外一个人出现的问题现象基本一致
https://github.com/fengcbo/pagehelper-spring-boot

@White-Duke White-Duke changed the title PageHelper在SpringBoot的Bean初始化的@PostConstruct 中不生效 PageHelper在SpringBoot的@PostConstruct 中不生效 Apr 9, 2018
@White-Duke
Copy link
Author

我现在自己实现了配置方法,没有使用starter了,还有一种方式是,直接定义一个PageInterceptor Bean就可以了。

@configuration
@order(Ordered.HIGHEST_PRECEDENCE)
@EnableConfigurationProperties(PageHelperProperties.class)
public class PageHelperConfig {

@Autowired
private PageHelperProperties pageHelperProperties;

@Bean
public BeanPostProcessor pageHelperBeanPostProcessor() {
    PageInterceptor interceptor = new PageInterceptor();
    interceptor.setProperties(pageHelperProperties.getProperties());

    return new BeanPostProcessor() {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof SqlSessionFactory) {
                SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) bean;
                if (!sqlSessionFactory.getConfiguration().getInterceptors().contains(interceptor)) {
                    sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
                }
            }
            return bean;
        }
    };
}

}

@abel533
Copy link
Collaborator

abel533 commented Apr 10, 2018

如果配合 通用 Mapper 使用,可以用 tk.mybatis.mapper.autoconfigure.ConfigurationCustomizer 接口,如果没用 Mapper,可以用 org 版本的。

@suiyueqiannian
Copy link

使用了通用 Mapper 后,通用 Mapper 中排除掉了 mybatis 官方的 starter 会导致这个情况,@Configuration @ConditionalOnBean({SqlSessionFactory.class}) @EnableConfigurationProperties({PageHelperProperties.class}) @AutoConfigureAfter({MybatisAutoConfiguration.class}) public class PageHelperAutoConfiguration {
这个使用了 mybatis 官方的 MybatisAutoConfiguration

@JR--Chen
Copy link

JR--Chen commented Aug 9, 2023

所以这个最后的解决办法是什么,我看了半天都没看明白

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

4 participants