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

@Autowired/@Value does not work on inner bean in native #29803

Closed
zcw159357 opened this issue Jan 11, 2023 · 5 comments
Closed

@Autowired/@Value does not work on inner bean in native #29803

zcw159357 opened this issue Jan 11, 2023 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Milestone

Comments

@zcw159357
Copy link

when using @Value or @Autowired inside a @Webfilter annotationed class and run in aot mode, it'll raise error

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named '(inner bean)#5884a914' that could not be found.


Action:

Consider defining a bean named '(inner bean)#5884a914' in your configuration.

spring boot version 3.0.1

example project here
demo.zip

seems in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#obtainInstanceFromSupplier

if (supplier instanceof InstanceSupplier<?> instanceSupplier) {
	return instanceSupplier.get(RegisteredBean.of((ConfigurableListableBeanFactory) this, beanName));
}

inner beans can't be found by beanName, but WebFilterHandler uses inner bean to create filter.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 11, 2023
@zcw159357 zcw159357 changed the title @webfilter with @Value does not work in native @Webfilter with @Value does not work in native Jan 11, 2023
@bclozel bclozel transferred this issue from spring-projects/spring-boot Jan 11, 2023
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing labels Jan 11, 2023
@sdeleuze sdeleuze added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 7, 2023
@sdeleuze sdeleuze added this to the 6.0.5 milestone Feb 7, 2023
@sdeleuze sdeleuze self-assigned this Feb 7, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 7, 2023

I can indeed reproduce the issue.

@sdeleuze sdeleuze modified the milestones: 6.0.5, 6.0.x Feb 7, 2023
@sdeleuze sdeleuze modified the milestones: 6.0.x, 6.0.7 Feb 28, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Mar 1, 2023

@bclozel cc @wilkinsona If I upgrade the repro to Spring Boot 3.0.3, I see the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Code generation is not supported for bean definitions declaring an instance supplier callback : Root bean: class [org.springframework.boot.web.servlet.ServletComponentRegisteringPostProcessor]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null
        at org.springframework.beans.factory.aot.BeanDefinitionMethodGenerator.<init>(BeanDefinitionMethodGenerator.java:82)
        at org.springframework.beans.factory.aot.BeanDefinitionMethodGeneratorFactory.getBeanDefinitionMethodGenerator(BeanDefinitionMethodGeneratorFactory.java:100)
        at org.springframework.beans.factory.aot.BeanDefinitionMethodGeneratorFactory.getBeanDefinitionMethodGenerator(BeanDefinitionMethodGeneratorFactory.java:115)

I am wondering if ServletComponentRegisteringPostProcessor needs special handling via an AOT processor like we did for similar cases, so maybe this issue should be moved back to Boot?

@sdeleuze sdeleuze removed this from the 6.0.7 milestone Mar 1, 2023
@sdeleuze sdeleuze added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 1, 2023
@wilkinsona
Copy link
Member

wilkinsona commented Mar 2, 2023

We definitely need to do something in Boot. ServletComponentRegisteringPostProcessor shouldn't be needed in an AOT-processed context as it would only try to register its beans again. It can be excluded with a BeanRegistrationExcludeFilter registered in META-INF/spring/aot.factories:

class ServletComponentRegisteringPostProcessorExcludeFilter implements BeanRegistrationExcludeFilter {

	@Override
	public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
		return "servletComponentRegisteringPostProcessor".equals(registeredBean.getBeanName());
	}

}

We then get back to the original failure:

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named '(inner bean)#61dbfa83' that could not be found.


Action:

Consider defining a bean named '(inner bean)#61dbfa83' in your configuration.

It would certainly be nice if Framework could support this arrangement but I don't know if that's possible. The code that's generated inFilterRegistrationBean__BeanDefinitions, TestFilter__BeanDefinitions, and TestFilter__Autowiring looks pretty good to me and it would be a shame if Boot had to use some custom generation.

Could you please explore what's possible on the Framework side when the exclude filter's in place? That will hopefully allow us to figure out if we just need the exclusion built into Boot, and Framework can tackle the rest of the problem, or if we need more extensive changes in Boot.

@sdeleuze sdeleuze added this to the 6.0.7 milestone Mar 6, 2023
@sdeleuze sdeleuze removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 6, 2023
@sdeleuze sdeleuze assigned jhoeller and unassigned sdeleuze Mar 6, 2023
@jhoeller jhoeller changed the title @Webfilter with @Value does not work in native @Value does not work on inner bean in native Mar 8, 2023
@jhoeller jhoeller changed the title @Value does not work on inner bean in native @Autowired/@Value does not work on inner bean in native Mar 8, 2023
@wilkinsona
Copy link
Member

Thanks, @jhoeller.

With Framework 6.0.7 snapshots and the exclude filter described above the problem no longer occurs. I've opened spring-projects/spring-boot#34563 so that Boot takes care of the exclusion automatically.

@zcw159357
Copy link
Author

Thank you all for the work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Projects
None yet
Development

No branches or pull requests

6 participants