Skip to content

Prevent bean eager initialization on MapperScannerConfigurer #453

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 1 commit into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2019 the original author or authors.
* Copyright 2010-2020 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.
Expand Down Expand Up @@ -191,7 +191,7 @@ public void setMarkerInterface(Class<?> superClass) {
* Specifies which {@code SqlSessionTemplate} to use in the case that there is more than one in the spring context.
* Usually this is only needed when you have more than one datasource.
* <p>
*
*
* @deprecated Use {@link #setSqlSessionTemplateBeanName(String)} instead
*
* @param sqlSessionTemplate
Expand Down Expand Up @@ -222,7 +222,7 @@ public void setSqlSessionTemplateBeanName(String sqlSessionTemplateName) {
* Specifies which {@code SqlSessionFactory} to use in the case that there is more than one in the spring context.
* Usually this is only needed when you have more than one datasource.
* <p>
*
*
* @deprecated Use {@link #setSqlSessionFactoryBeanName(String)} instead.
*
* @param sqlSessionFactory
Expand Down Expand Up @@ -253,7 +253,7 @@ public void setSqlSessionFactoryBeanName(String sqlSessionFactoryName) {
* Specifies a flag that whether execute a property placeholder processing or not.
* <p>
* The default is {@literal false}. This means that a property placeholder processing does not execute.
*
*
* @since 1.1.1
*
* @param processPropertyPlaceHolders
Expand Down Expand Up @@ -329,7 +329,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

/**
* {@inheritDoc}
*
*
* @since 1.0.2
*/
@Override
Expand Down Expand Up @@ -364,7 +364,8 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
* definition. Then update the values.
*/
private void processPropertyPlaceHolders() {
Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class,
false, false);

if (!prcs.isEmpty() && applicationContext instanceof ConfigurableApplicationContext) {
BeanDefinition mapperScannerBean = ((ConfigurableApplicationContext) applicationContext).getBeanFactory()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2019 the original author or authors.
* Copyright 2010-2020 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.
Expand Down Expand Up @@ -43,6 +43,7 @@
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
Expand Down Expand Up @@ -400,6 +401,7 @@ public static class AppConfigWithMapperScanIsRepeat {
public static class AppConfigWithMapperScans {
}

@ComponentScan("org.mybatis.spring.annotation.factory")
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1", lazyInitialization = "${mybatis.lazy-initialization:false}")
public static class LazyConfigWithPropertySourcesPlaceholderConfigurer {
@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2010-2020 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
*
* http://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.annotation.factory;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class SimpleFactoryBean implements FactoryBean<Object> {

private static boolean isInitializedEarly = false;

public SimpleFactoryBean() {
isInitializedEarly = true;
throw new RuntimeException();
}

@Autowired
public SimpleFactoryBean(ApplicationContext context) {
if (isInitializedEarly) {
throw new RuntimeException();
}
}

public Object getObject() {
return new Object();
}

public Class<?> getObjectType() {
return Object.class;
}

}