Skip to content
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

MapperScannerConfigurer cause early initialize bean #452

Closed
huangxfchn opened this issue Mar 11, 2020 · 10 comments · Fixed by #453
Closed

MapperScannerConfigurer cause early initialize bean #452

huangxfchn opened this issue Mar 11, 2020 · 10 comments · Fixed by #453
Assignees
Labels
Milestone

Comments

@huangxfchn
Copy link

huangxfchn commented Mar 11, 2020

description

Since 2.0.2, MapperScannerConfigurer has changed a lot, but it brings bug.

Mapperscannerconfigurer belongs to beandefinitionregistrypostprocessor. During this period, the bean has not been initialized, but the processpropertyplaceholders() method traverses the entire ApplicationContext, leading to the bean being instantiated in advance, because ApplicationContext.getBeansOfType() will be created in advance by default.

If the bean is instantiated in advance, it will cause some problems, such as unable to rely on injection, failure to get configuration parameters, etc

code

MapperScannerConfigurer.java

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
    if (this.processPropertyPlaceHolders) {
      processPropertyPlaceHolders();
    }
    //……
}

private void processPropertyPlaceHolders() {
	Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
	//……
}
@kazuki43zoo
Copy link
Member

@huangxfchn Thanks for your reporting!!

Could your provide a project for reproducing bug via GitHub?

@kazuki43zoo kazuki43zoo self-assigned this Mar 11, 2020
@huangxfchn
Copy link
Author

@huangxfchn Thanks for your reporting!!

Could your provide a project for reproducing bug via GitHub?

OK, tomorrow, please. I'm off work today.

@kazuki43zoo
Copy link
Member

thx!!

@xiongmaodada
Copy link

@kazuki43zoo hi, I provide a project for reproducing bug via Github.

when run this demo, result is as follow:

========consumerConfig is null 

tag: factoryBean consumerFactoryBean object without injecting consumerConfig parameter.

while modify mybatis-spring-boot-starter version

dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

then running this demo, result is as follow:

========consumerConfig is not null 

tag: factoryBean consumerFactoryBean object with injecting consumerConfig successfully.

@huangxfchn
Copy link
Author

@kazuki43zoo hi, I provide a project for reproducing bug via Github.

when run this demo, result is as follow:

========consumerConfig is null 

tag: factoryBean consumerFactoryBean object without injecting consumerConfig parameter.

while modify mybatis-spring-boot-starter version

dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

then running this demo, result is as follow:

========consumerConfig is not null 

tag: factoryBean consumerFactoryBean object with injecting consumerConfig successfully.

Wowo, Thank you very much!

@huangxfchn
Copy link
Author

huangxfchn commented Mar 14, 2020

like this: spring boot test cause early FactoryBean instantiation #15898

@xiongmaodada
Copy link

the above address is as followed:

No results matched your search.

@huangxfchn
Copy link
Author

the above address is as followed:

No results matched your search.

Sorry, I made a mistake. The correct link: spring boot issue#15898

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Mar 15, 2020

@huangxfchn @xiongmaodada

Thanks for your contribution!! This problem which potentially existed from previous versions, became apparent with the support of gh-376.
For resolve this problem, I've fixed to prevent eager initialization of bean when retrieve PropertyResourceConfigurer on the MapperScannerConfigurer.

The 2.0.5-SNAPSHOT is available via OSS NEXUS snapshot repository.

Workaround until released the 2.0.5

This problem can prevent by set the processPropertyPlaceHolders to false as follow:

@Configuration
static class MyBatisConfig {
	@Bean
	MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer configurer = new MapperScannerConfigurer();
		configurer.setBasePackage("com.example.mybatis");
		configurer.setProcessPropertyPlaceHolders(false); // set to 'false'
		return configurer;
	}
}

Note

The mybatis-spring-boot-starter 2.1.3-SNAPSHOT is available via OSS NEXUS snapshot repository.

@ryou-ks
Copy link

ryou-ks commented Dec 9, 2020

there is an error happened when I used mybatis-spring-2.0.5/spring 5.2.6/mybatis3.5.5 as follow:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'targetDataSource' available

How to solve this problem?


my spring xml code

<bean>
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
      <property name="locations" />
	<list>
           <value>classpath:mybatis/database.properties<value/>
        <list>
      <property/>
   </bean>
   <bean id="targetDataSource" class="org.springframework.jndi.jndiObjectFactoryBean">
 <beans/>


---------------------------------------------------------------------------------------------------
my properties (database.properties)
targetDataSource.jndiName=aa/bb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants