diff --git a/mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java b/mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java index 94fbcc7d2..8a2a78383 100644 --- a/mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java +++ b/mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java @@ -84,6 +84,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.support.SimpleThreadScope; +import org.springframework.core.annotation.Order; /** * Tests for {@link MybatisAutoConfiguration} @@ -323,13 +324,34 @@ void testDefaultBootConfiguration() { } @Test - void testWithInterceptors() { + void testWithInterceptorsOrder1() { this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration.class, MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1); - assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(1); + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2); + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0)) + .isInstanceOf(MyInterceptor2.class); + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1)) + .isInstanceOf(MyInterceptor.class); + + this.context.close(); + } + + @Test + void testWithInterceptorsOrder2() { + this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration2.class, + MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1); + assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1); + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2); + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0)) + .isInstanceOf(MyInterceptor.class); + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1)) + .isInstanceOf(MyInterceptor2.class); + this.context.close(); } @@ -365,8 +387,9 @@ void testMixedWithConfigurationFileAndInterceptor() { assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1); assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1); assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000); - assertThat(configuration.getInterceptors()).hasSize(1); - assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class); + assertThat(configuration.getInterceptors()).hasSize(2); + assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class); + assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class); } @Test @@ -462,8 +485,9 @@ void testMixedWithFullConfigurations() { assertThat(configuration.getMappedStatementNames()) .contains("org.mybatis.spring.boot.autoconfigure.mapper.CityMapper.findById"); assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.REUSE); - assertThat(configuration.getInterceptors()).hasSize(1); - assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class); + assertThat(configuration.getInterceptors()).hasSize(2); + assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class); + assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class); assertThat(configuration.getDatabaseId()).isEqualTo("h2"); } @@ -803,10 +827,35 @@ public CityMapperImpl cityMapper() { static class MybatisInterceptorConfiguration { @Bean + @Order(2) public MyInterceptor myInterceptor() { return new MyInterceptor(); } + @Bean + @Order(1) + public MyInterceptor2 myInterceptor2() { + return new MyInterceptor2(); + } + + } + + @Configuration + @EnableAutoConfiguration + static class MybatisInterceptorConfiguration2 { + + @Bean + @Order(1) + public MyInterceptor myInterceptor() { + return new MyInterceptor(); + } + + @Bean + @Order(2) + public MyInterceptor2 myInterceptor2() { + return new MyInterceptor2(); + } + } @Configuration @@ -870,6 +919,25 @@ public void setProperties(Properties properties) { } } + @Intercepts(@Signature(type = Map.class, method = "get", args = { Object.class })) + static class MyInterceptor2 implements Interceptor { + + @Override + public Object intercept(Invocation invocation) { + return "Test2"; + } + + @Override + public Object plugin(Object target) { + return Plugin.wrap(target, this); + } + + @Override + public void setProperties(Properties properties) { + + } + } + @Configuration static class DatabaseProvidersConfiguration {