|
84 | 84 | import org.springframework.context.annotation.Configuration; |
85 | 85 | import org.springframework.context.annotation.Primary; |
86 | 86 | import org.springframework.context.support.SimpleThreadScope; |
| 87 | +import org.springframework.core.annotation.Order; |
87 | 88 |
|
88 | 89 | /** |
89 | 90 | * Tests for {@link MybatisAutoConfiguration} |
@@ -323,13 +324,34 @@ void testDefaultBootConfiguration() { |
323 | 324 | } |
324 | 325 |
|
325 | 326 | @Test |
326 | | - void testWithInterceptors() { |
| 327 | + void testWithInterceptorsOrder1() { |
327 | 328 | this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration.class, |
328 | 329 | MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); |
329 | 330 | this.context.refresh(); |
330 | 331 | assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1); |
331 | 332 | assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1); |
332 | | - assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(1); |
| 333 | + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2); |
| 334 | + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0)) |
| 335 | + .isInstanceOf(MyInterceptor2.class); |
| 336 | + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1)) |
| 337 | + .isInstanceOf(MyInterceptor.class); |
| 338 | + |
| 339 | + this.context.close(); |
| 340 | + } |
| 341 | + |
| 342 | + @Test |
| 343 | + void testWithInterceptorsOrder2() { |
| 344 | + this.context.register(EmbeddedDataSourceConfiguration.class, MybatisInterceptorConfiguration2.class, |
| 345 | + MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); |
| 346 | + this.context.refresh(); |
| 347 | + assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1); |
| 348 | + assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1); |
| 349 | + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors()).hasSize(2); |
| 350 | + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(0)) |
| 351 | + .isInstanceOf(MyInterceptor.class); |
| 352 | + assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().getInterceptors().get(1)) |
| 353 | + .isInstanceOf(MyInterceptor2.class); |
| 354 | + |
333 | 355 | this.context.close(); |
334 | 356 | } |
335 | 357 |
|
@@ -365,8 +387,9 @@ void testMixedWithConfigurationFileAndInterceptor() { |
365 | 387 | assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1); |
366 | 388 | assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1); |
367 | 389 | assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000); |
368 | | - assertThat(configuration.getInterceptors()).hasSize(1); |
369 | | - assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class); |
| 390 | + assertThat(configuration.getInterceptors()).hasSize(2); |
| 391 | + assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class); |
| 392 | + assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class); |
370 | 393 | } |
371 | 394 |
|
372 | 395 | @Test |
@@ -462,8 +485,9 @@ void testMixedWithFullConfigurations() { |
462 | 485 | assertThat(configuration.getMappedStatementNames()) |
463 | 486 | .contains("org.mybatis.spring.boot.autoconfigure.mapper.CityMapper.findById"); |
464 | 487 | assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.REUSE); |
465 | | - assertThat(configuration.getInterceptors()).hasSize(1); |
466 | | - assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor.class); |
| 488 | + assertThat(configuration.getInterceptors()).hasSize(2); |
| 489 | + assertThat(configuration.getInterceptors().get(0)).isInstanceOf(MyInterceptor2.class); |
| 490 | + assertThat(configuration.getInterceptors().get(1)).isInstanceOf(MyInterceptor.class); |
467 | 491 | assertThat(configuration.getDatabaseId()).isEqualTo("h2"); |
468 | 492 | } |
469 | 493 |
|
@@ -803,10 +827,35 @@ public CityMapperImpl cityMapper() { |
803 | 827 | static class MybatisInterceptorConfiguration { |
804 | 828 |
|
805 | 829 | @Bean |
| 830 | + @Order(2) |
806 | 831 | public MyInterceptor myInterceptor() { |
807 | 832 | return new MyInterceptor(); |
808 | 833 | } |
809 | 834 |
|
| 835 | + @Bean |
| 836 | + @Order(1) |
| 837 | + public MyInterceptor2 myInterceptor2() { |
| 838 | + return new MyInterceptor2(); |
| 839 | + } |
| 840 | + |
| 841 | + } |
| 842 | + |
| 843 | + @Configuration |
| 844 | + @EnableAutoConfiguration |
| 845 | + static class MybatisInterceptorConfiguration2 { |
| 846 | + |
| 847 | + @Bean |
| 848 | + @Order(1) |
| 849 | + public MyInterceptor myInterceptor() { |
| 850 | + return new MyInterceptor(); |
| 851 | + } |
| 852 | + |
| 853 | + @Bean |
| 854 | + @Order(2) |
| 855 | + public MyInterceptor2 myInterceptor2() { |
| 856 | + return new MyInterceptor2(); |
| 857 | + } |
| 858 | + |
810 | 859 | } |
811 | 860 |
|
812 | 861 | @Configuration |
@@ -870,6 +919,25 @@ public void setProperties(Properties properties) { |
870 | 919 | } |
871 | 920 | } |
872 | 921 |
|
| 922 | + @Intercepts(@Signature(type = Map.class, method = "get", args = { Object.class })) |
| 923 | + static class MyInterceptor2 implements Interceptor { |
| 924 | + |
| 925 | + @Override |
| 926 | + public Object intercept(Invocation invocation) { |
| 927 | + return "Test2"; |
| 928 | + } |
| 929 | + |
| 930 | + @Override |
| 931 | + public Object plugin(Object target) { |
| 932 | + return Plugin.wrap(target, this); |
| 933 | + } |
| 934 | + |
| 935 | + @Override |
| 936 | + public void setProperties(Properties properties) { |
| 937 | + |
| 938 | + } |
| 939 | + } |
| 940 | + |
873 | 941 | @Configuration |
874 | 942 | static class DatabaseProvidersConfiguration { |
875 | 943 |
|
|
0 commit comments