-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Support excluding filter on mapper scan feature #836
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7f7ef3d
fix issue 548. add ComponentScan.Filter[] excludeFilters() to @Mapper…
apdya 7000d7f
Merge branch 'master' into scanfilter
apdya cc31d4a
optimize imports
apdya c711ce4
1.remove @Deprecated on method setResourceLoader.
apdya 789a8e3
replace start import and format code
apdya eb9e79e
move package 'filter' from org.mybatis.spring.scan to org.mybatis.spring
apdya f3344d7
fix combined exclude filter and add combined filter test
apdya 7fefb72
modify exception prompt message and add exception test code
apdya f449b4c
code format
apdya 756531a
support exclude filter on mapper scan with xml
apdya 564c108
add more test
apdya 3e67f25
Optimized process to parse excludeFilters
apdya 82ef68f
Optimized process to parse excludeFilters and add 'processPropertyPla…
apdya 56990f6
Merge branch 'master' into scanfilter
apdya b7f3f35
fix ci fail in windows
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/org/mybatis/spring/scan/filter/ScanFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2010-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mybatis.spring.scan.filter; | ||
|
||
import com.mockrunner.mock.jdbc.MockDataSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.mybatis.spring.SqlSessionFactoryBean; | ||
import org.mybatis.spring.scan.filter.config.AppConfig; | ||
import org.springframework.beans.factory.support.GenericBeanDefinition; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* test the function of excludeFilters in @MapperScan | ||
*/ | ||
public class ScanFilterTest { | ||
|
||
private AnnotationConfigApplicationContext applicationContext; | ||
|
||
|
||
@Test | ||
void testCustomScanFilter() { | ||
startContext(AppConfig.CustomFilterConfig.class); | ||
// use org.mybatis.spring.scan.filter.datasource as basePackages and exclude package datasource2 by MapperScan.excludeFilters | ||
// mapper in package datasource2 will not be registered to beanFactory | ||
assertThat(applicationContext.containsBean("dataSource2Mapper")).isEqualTo(false); | ||
|
||
// mapper in package datasource except datasource2 will be registered to beanFactory correctly. | ||
assertThat(applicationContext.containsBean("commonDataSourceMapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource1Mapper")).isEqualTo(true); | ||
} | ||
|
||
@Test | ||
void testAnnoScanFilter() { | ||
startContext(AppConfig.AnnoFilterConfig.class); | ||
|
||
// use @AnnoTypeFilter to exclude mapper | ||
assertThat(applicationContext.containsBean("annoExcludeMapper")).isEqualTo(false); | ||
|
||
// mapper in package datasource except datasource2 will be registered to beanFactory correctly. | ||
assertThat(applicationContext.containsBean("commonDataSourceMapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource1Mapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource2Mapper")).isEqualTo(true); | ||
} | ||
|
||
|
||
@Test | ||
void testAssignableScanFilter() { | ||
startContext(AppConfig.AssignableFilterConfig.class); | ||
|
||
// exclude AssignableMapper by AssignableFilter | ||
assertThat(applicationContext.containsBean("assignableMapper")).isEqualTo(false); | ||
|
||
// mapper in package datasource except datasource2 will be registered to beanFactory correctly. | ||
assertThat(applicationContext.containsBean("commonDataSourceMapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource1Mapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource2Mapper")).isEqualTo(true); | ||
} | ||
|
||
@Test | ||
void testRegexScanFilter() { | ||
startContext(AppConfig.RegexFilterConfig.class); | ||
|
||
// exclude package datasource1 by Regex | ||
assertThat(applicationContext.containsBean("dataSource1Mapper")).isEqualTo(false); | ||
|
||
// mapper in package datasource except datasource1 will be registered to beanFactory correctly. | ||
assertThat(applicationContext.containsBean("commonDataSourceMapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource2Mapper")).isEqualTo(true); | ||
} | ||
|
||
@Test | ||
void testAspectJScanFilter() { | ||
|
||
startContext(AppConfig.AspectJFilterConfig.class); | ||
|
||
// exclude dataSource1Mapper by AspectJ | ||
assertThat(applicationContext.containsBean("dataSource1Mapper")).isEqualTo(false); | ||
|
||
// mapper in package datasource except datasource1 will be registered to beanFactory correctly. | ||
assertThat(applicationContext.containsBean("commonDataSourceMapper")).isEqualTo(true); | ||
assertThat(applicationContext.containsBean("dataSource2Mapper")).isEqualTo(true); | ||
} | ||
|
||
|
||
private void startContext(Class<?> config) { | ||
applicationContext = new AnnotationConfigApplicationContext(); | ||
// use @MapperScan with excludeFilters in AppConfig.class | ||
applicationContext.register(config); | ||
setupSqlSessionFactory("sqlSessionFactory"); | ||
applicationContext.refresh(); | ||
applicationContext.start(); | ||
} | ||
|
||
|
||
private void setupSqlSessionFactory(String name) { | ||
GenericBeanDefinition definition = new GenericBeanDefinition(); | ||
definition.setBeanClass(SqlSessionFactoryBean.class); | ||
definition.getPropertyValues().add("dataSource", new MockDataSource()); | ||
applicationContext.registerBeanDefinition(name, definition); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.