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

Bean definition overridden when spring.aot.enabled=true in spring boot 3.0 #780

Closed
wylswz opened this issue Jan 28, 2023 · 3 comments · Fixed by #818
Closed

Bean definition overridden when spring.aot.enabled=true in spring boot 3.0 #780

wylswz opened this issue Jan 28, 2023 · 3 comments · Fixed by #818
Assignees
Labels
Milestone

Comments

@wylswz
Copy link

wylswz commented Jan 28, 2023

original discussion

The project is compiled with native profile and started with -Dspring.aot.enabled=true, and I got following exception.

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'l1ServiceA' defined in com.xmbsmdsj.springbootdemo.NativeConfiguration: @Bean definition illegally overridden by existing bean definition: Root bean: class [com.xmbsmdsj.springbootdemo.L1ServiceA]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=l1ServiceA; initMethodNames=null; destroyMethodNames=null
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.isOverriddenByExistingDefinition(ConfigurationClassBeanDefinitionReader.java:319) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:204) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:144) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:410) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:283) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:145) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:745) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:565) ~[spring-context-6.0.3.jar:6.0.3]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.1.jar:3.0.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.1.jar:3.0.1]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.1.jar:3.0.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.1.jar:3.0.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.1.jar:3.0.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.1.jar:3.0.1]
	at com.xmbsmdsj.springbootdemo.SpringBootDemoApplication.main(SpringBootDemoApplication.java:11) ~[classes/:na]

I dropped a breakpoint in following method of ClassPathBeanDefinitionScanner, observing that @configuration classes seem to be scanned even if bean definition registrations are generated at build-time.

	public int scan(String... basePackages) {
		int beanCountAtScanStart = this.registry.getBeanDefinitionCount();

		doScan(basePackages);

		// It looks like the issue was due to this block
		if (this.includeAnnotationConfig) {
			AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
		}
		return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
	}

Heres the code to reproduce
https://github.com/spring-projects/spring-boot/files/10471672/spring-boot-demo.zip

@wylswz wylswz changed the title Bean definition overridden when spring.aot.enabled=true Bean definition overridden when spring.aot.enabled=true in spring boot 3.0 Jan 28, 2023
@kazuki43zoo kazuki43zoo self-assigned this May 13, 2023
@kazuki43zoo kazuki43zoo added this to the 3.0.2 milestone May 13, 2023
@kazuki43zoo
Copy link
Member

kazuki43zoo commented May 13, 2023

@wylswz

Thanks for your contributing!
Probably, this issue can be fix to use the AotDetector#useGeneratedArtifacts() instead of NativeDetector.inNativeImage() for set the includeAnnotationConfig to false on the ClassPathMapperScanner.

Related: gh-757

kazuki43zoo added a commit to kazuki43zoo/spring that referenced this issue May 13, 2023
Fixes mybatisgh-780

Fix to use the AotDetector#useGeneratedArtifacts() instead of NativeDetector.inNativeImage() for set the includeAnnotationConfig to false on the ClassPathMapperScanner
kazuki43zoo added a commit that referenced this issue May 13, 2023
Support spring.aot.enabled=true
@kazuki43zoo
Copy link
Member

@wylswz

I've fixed this issue and I Introduce how to build the native image with mybatis-spring-boot-starter.

@wylswz
Copy link
Author

wylswz commented May 13, 2023

Thanks for your efforts, will try it out.

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.

2 participants