Skip to content

Commit 82ef68f

Browse files
committed
Optimized process to parse excludeFilters and add 'processPropertyPlaceHolders' turn off test
1 parent 3e67f25 commit 82ef68f

File tree

6 files changed

+96
-4
lines changed

6 files changed

+96
-4
lines changed

src/main/java/org/mybatis/spring/mapper/MapperScannerConfigurer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
407407
scanner.setAddToConfig(this.addToConfig);
408408
scanner.setAnnotationClass(this.annotationClass);
409409
scanner.setMarkerInterface(this.markerInterface);
410-
scanner.setExcludeFilters(this.excludeFilters);
410+
scanner.setExcludeFilters(this.excludeFilters = mergeExcludeFilters());
411411
scanner.setSqlSessionFactory(this.sqlSessionFactory);
412412
scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
413413
scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
@@ -467,7 +467,6 @@ private void processPropertyPlaceHolders() {
467467
this.lazyInitialization = Optional.ofNullable(this.lazyInitialization).map(getEnvironment()::resolvePlaceholders)
468468
.orElse(null);
469469
this.defaultScope = Optional.ofNullable(this.defaultScope).map(getEnvironment()::resolvePlaceholders).orElse(null);
470-
this.excludeFilters = mergeExcludeFilters();
471470
}
472471

473472
private Environment getEnvironment() {
@@ -528,7 +527,9 @@ private List<TypeFilter> mergeExcludeFilters() {
528527
private TypeFilter createTypeFilter(String filterType, String expression, @Nullable ClassLoader classLoader)
529528
throws ClassNotFoundException {
530529

531-
expression = this.getEnvironment().resolvePlaceholders(expression);
530+
if (this.processPropertyPlaceHolders) {
531+
expression = this.getEnvironment().resolvePlaceholders(expression);
532+
}
532533

533534
switch (filterType) {
534535
case "annotation":

src/test/java/org/mybatis/spring/filter/ScanFilterTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import com.mockrunner.mock.jdbc.MockDataSource;
2222

23+
import java.util.regex.PatternSyntaxException;
24+
2325
import org.junit.jupiter.api.Test;
2426
import org.mybatis.spring.SqlSessionFactoryBean;
2527
import org.mybatis.spring.filter.config.AppConfig;
@@ -189,6 +191,19 @@ void invalidPropertyClasses() {
189191
() -> startContext(AppConfig.RegexTypeWithClassesPropertyConfig.class));
190192
}
191193

194+
@Test
195+
void processPropertyPlaceHoldersSwitchTest() {
196+
// if processPropertyPlaceHolders turn off regex compile will fail
197+
assertThrows(PatternSyntaxException.class,
198+
() -> startContext(AppConfig.ProcessPropertyPlaceHoldersOffConfig.class));
199+
}
200+
201+
@Test
202+
void processPropertyPlaceHoldersSwitchTest1() {
203+
// processPropertyPlaceHolders turn off has no effect to FilterType which don't use pattern property
204+
startContext(AppConfig.AnnoFilterWithProcessPlaceHolderOffConfig.class);
205+
}
206+
192207
private void startContext(Class<?> config) {
193208
applicationContext = new AnnotationConfigApplicationContext();
194209
// use @MapperScan with excludeFilters in AppConfig.class

src/test/java/org/mybatis/spring/filter/config/AppConfig.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,21 @@ static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
121121
}
122122

123123
}
124+
125+
@MapperScan(basePackages = "org.mybatis.spring.filter.datasource", processPropertyPlaceHolders = false, excludeFilters = {
126+
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "${exclude-filters.regex}") })
127+
public static class ProcessPropertyPlaceHoldersOffConfig {
128+
@Bean
129+
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
130+
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
131+
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/filter/config/application.properties"));
132+
return configurer;
133+
}
134+
}
135+
136+
@MapperScan(basePackages = "org.mybatis.spring.filter.datasource", processPropertyPlaceHolders = false, excludeFilters = {
137+
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = AnnoTypeFilter.class) })
138+
public static class AnnoFilterWithProcessPlaceHolderOffConfig {
139+
140+
}
124141
}

src/test/java/org/mybatis/spring/filter/xml/XmlScanFilterTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21+
import java.util.regex.PatternSyntaxException;
22+
2123
import org.junit.jupiter.api.Test;
2224
import org.springframework.context.support.ClassPathXmlApplicationContext;
2325

@@ -138,6 +140,14 @@ void warpedClassNotFoundException() {
138140
closeContext();
139141
}
140142

143+
@Test
144+
void processPropertyPlaceHoldersSwitchTest() {
145+
// if processPropertyPlaceHolders turn off regex compile will fail
146+
assertThrows(PatternSyntaxException.class,
147+
() -> startContext("org/mybatis/spring/filter/xml/appContextProcessPlaceHolderOff.xml"));
148+
closeContext();
149+
}
150+
141151
private void startContext(String config) {
142152
applicationContext = new ClassPathXmlApplicationContext(config);
143153
applicationContext.refresh();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2010-2023 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!--
20+
<mybatis:scan> with new property exclude-filter
21+
-->
22+
<beans xmlns="http://www.springframework.org/schema/beans"
23+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24+
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
25+
xmlns:context="http://www.springframework.org/schema/context"
26+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
27+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
28+
29+
<!-- in-memory database and a datasource -->
30+
<jdbc:embedded-database id="dataSource">
31+
<jdbc:script location="classpath:org/mybatis/spring/batch/db/database-schema.sql"/>
32+
<jdbc:script location="classpath:org/mybatis/spring/batch/db/database-test-data.sql"/>
33+
</jdbc:embedded-database>
34+
35+
36+
<!-- simplest possible SqlSessionFactory configuration -->
37+
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
38+
<property name="dataSource" ref="dataSource"/>
39+
</bean>
40+
41+
<context:property-placeholder location="classpath:/org/mybatis/spring/filter/xml/default.properties"/>
42+
43+
<mybatis:scan base-package="org.mybatis.spring.filter.datasource" process-property-placeholders="false">
44+
<mybatis:exclude-filter type="regex"
45+
expression="${filter.regex}"/>
46+
</mybatis:scan>
47+
48+
</beans>

src/test/java/org/mybatis/spring/filter/xml/default.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
#
1616

1717
filter.custom=org.mybatis.spring.filter.customfilter.CustomTypeFilter
18-
filter.aspectj=*..CommonDataSourceMapper
18+
filter.aspectj=*..CommonDataSourceMapper
19+
filter.regex=org\.mybatis\.spring\.filter\.datasource\.datasource1\..*

0 commit comments

Comments
 (0)