Skip to content

Commit f2d2aed

Browse files
authored
Merge pull request #453 from kazuki43zoo/gh-452
Prevent bean eager initialization on MapperScannerConfigurer
2 parents 5a72486 + af6c0b3 commit f2d2aed

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2019 the original author or authors.
2+
* Copyright 2010-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -191,7 +191,7 @@ public void setMarkerInterface(Class<?> superClass) {
191191
* Specifies which {@code SqlSessionTemplate} to use in the case that there is more than one in the spring context.
192192
* Usually this is only needed when you have more than one datasource.
193193
* <p>
194-
*
194+
*
195195
* @deprecated Use {@link #setSqlSessionTemplateBeanName(String)} instead
196196
*
197197
* @param sqlSessionTemplate
@@ -222,7 +222,7 @@ public void setSqlSessionTemplateBeanName(String sqlSessionTemplateName) {
222222
* Specifies which {@code SqlSessionFactory} to use in the case that there is more than one in the spring context.
223223
* Usually this is only needed when you have more than one datasource.
224224
* <p>
225-
*
225+
*
226226
* @deprecated Use {@link #setSqlSessionFactoryBeanName(String)} instead.
227227
*
228228
* @param sqlSessionFactory
@@ -253,7 +253,7 @@ public void setSqlSessionFactoryBeanName(String sqlSessionFactoryName) {
253253
* Specifies a flag that whether execute a property placeholder processing or not.
254254
* <p>
255255
* The default is {@literal false}. This means that a property placeholder processing does not execute.
256-
*
256+
*
257257
* @since 1.1.1
258258
*
259259
* @param processPropertyPlaceHolders
@@ -329,7 +329,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
329329

330330
/**
331331
* {@inheritDoc}
332-
*
332+
*
333333
* @since 1.0.2
334334
*/
335335
@Override
@@ -364,7 +364,8 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
364364
* definition. Then update the values.
365365
*/
366366
private void processPropertyPlaceHolders() {
367-
Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
367+
Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class,
368+
false, false);
368369

369370
if (!prcs.isEmpty() && applicationContext instanceof ConfigurableApplicationContext) {
370371
BeanDefinition mapperScannerBean = ((ConfigurableApplicationContext) applicationContext).getBeanFactory()

src/test/java/org/mybatis/spring/annotation/MapperScanTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2019 the original author or authors.
2+
* Copyright 2010-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@
4343
import org.springframework.beans.factory.support.GenericBeanDefinition;
4444
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4545
import org.springframework.context.annotation.Bean;
46+
import org.springframework.context.annotation.ComponentScan;
4647
import org.springframework.context.annotation.Configuration;
4748
import org.springframework.context.annotation.PropertySource;
4849
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@@ -400,6 +401,7 @@ public static class AppConfigWithMapperScanIsRepeat {
400401
public static class AppConfigWithMapperScans {
401402
}
402403

404+
@ComponentScan("org.mybatis.spring.annotation.factory")
403405
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1", lazyInitialization = "${mybatis.lazy-initialization:false}")
404406
public static class LazyConfigWithPropertySourcesPlaceholderConfigurer {
405407
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2010-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.spring.annotation.factory;
17+
18+
import org.springframework.beans.factory.FactoryBean;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.stereotype.Component;
22+
23+
@Component
24+
public class SimpleFactoryBean implements FactoryBean<Object> {
25+
26+
private static boolean isInitializedEarly = false;
27+
28+
public SimpleFactoryBean() {
29+
isInitializedEarly = true;
30+
throw new RuntimeException();
31+
}
32+
33+
@Autowired
34+
public SimpleFactoryBean(ApplicationContext context) {
35+
if (isInitializedEarly) {
36+
throw new RuntimeException();
37+
}
38+
}
39+
40+
public Object getObject() {
41+
return new Object();
42+
}
43+
44+
public Class<?> getObjectType() {
45+
return Object.class;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)