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

Unexpected Behavior with Lit Template Bean Initialization in Vaadin Flow #20627

Open
myronenkom opened this issue Dec 5, 2024 · 2 comments
Open

Comments

@myronenkom
Copy link

Description of the bug

I've encountered unexpected behavior with the Lit Template during the initialization of beans in a specific dependency structure. I'm unsure if this is expected behavior or a bug. Here's the scenario:

Scenario Description:

  • A Vaadin Lit bean injects another Vaadin bean component using the Lit @id annotation.
  • The injected component, in turn, injects another Lit bean via constructor injection.

Observed Behavior:

  • During initialization, an exception is thrown when constructing the described bean structure.
Stack Trace: A component specified to use a...

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'subTab1Content' defined in file [C:\Users\38068\IdeaProjects\issue-with-eventbus-and-spring-scope\target\classes\org\vaadin\example\littemplateissue\subtab\content\SubTab1Content.class]: Failed to instantiate [org.vaadin.example.littemplateissue.subtab.content.SubTab1Content]: Constructor threw exception
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1337) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:355) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1443) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:904) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:782) ~[spring-beans-6.1.15.jar:6.1.15]
	... 119 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.vaadin.example.littemplateissue.subtab.content.SubTab1Content]: Constructor threw exception
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:221) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:94) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1331) ~[spring-beans-6.1.15.jar:6.1.15]
	... 129 common frames omitted
Caused by: java.lang.IllegalArgumentException: A component specified to use a sub-tab1-content-layout element cannot use an element with tag name sub-tab2-lit
	at com.vaadin.flow.component.Component.mapToElement(Component.java:197) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.Component.<init>(Component.java:127) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.littemplate.LitTemplate.<init>(LitTemplate.java:93) ~[flow-lit-template-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.littemplate.LitTemplate.<init>(LitTemplate.java:81) ~[flow-lit-template-24.5.8.jar:24.5.8]
	at org.vaadin.example.littemplateissue.subtab.content.SubTab1ContentLayout.<init>(SubTab1ContentLayout.java:20) ~[classes/:na]
	at org.vaadin.example.littemplateissue.subtab.content.SubTab1Content.<init>(SubTab1Content.java:9) ~[classes/:na]
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[na:na]
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:195) ~[spring-beans-6.1.15.jar:6.1.15]
	... 131 common frames omitted

Expected behavior

Bean initialization should succeed without exceptions.

Minimal reproducible example

Clone the repository from this branch.
Run the project.
Navigate to the /root2 route.

Versions

  • Vaadin / Flow version: 24.5.7
  • Java version: 21
@mcollovati
Copy link
Collaborator

Adding an important part of the stack-trace missing in the issue description

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.vaadin.example.littemplateissue.subtab.SubTab2Lit': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'subTab1Content' defined in file [/[path-to-project]/kubernates-kit-issue/target/classes/org/vaadin/example/littemplateissue/subtab/content/SubTab1Content.class]: Failed to instantiate [org.vaadin.example.littemplateissue.subtab.content.SubTab1Content]: Constructor threw exception
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:795) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:237) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1375) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1212) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.15.jar:6.1.15]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:321) ~[spring-beans-6.1.15.jar:6.1.15]
	at com.vaadin.flow.spring.SpringInstantiator.createComponent(SpringInstantiator.java:90) ~[vaadin-spring-24.5.8.jar:na]
	at com.vaadin.flow.component.ComponentUtil.componentFromElement(ComponentUtil.java:536) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.Component.from(Component.java:570) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.template.internal.IdMapper.injectTemplateElement(IdMapper.java:172) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.template.internal.IdMapper.attachExistingElementById(IdMapper.java:156) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.template.internal.IdMapper.injectClientSideElement(IdMapper.java:108) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.template.internal.IdMapper.mapComponentOrElement(IdMapper.java:79) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.littemplate.LitTemplateInitializer.lambda$initChildElements$2(LitTemplateInitializer.java:108) ~[flow-lit-template-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.template.internal.ParserData.lambda$forEachInjectedField$0(ParserData.java:64) ~[flow-server-24.5.8.jar:24.5.8]
	at java.base/java.util.HashMap.forEach(HashMap.java:1430) ~[na:na]
	at java.base/java.util.Collections$UnmodifiableMap.forEach(Collections.java:1708) ~[na:na]
	at com.vaadin.flow.component.template.internal.ParserData.forEachInjectedField(ParserData.java:63) ~[flow-server-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.littemplate.LitTemplateInitializer.initChildElements(LitTemplateInitializer.java:107) ~[flow-lit-template-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.littemplate.LitTemplate.<init>(LitTemplate.java:96) ~[flow-lit-template-24.5.8.jar:24.5.8]
	at com.vaadin.flow.component.littemplate.LitTemplate.<init>(LitTemplate.java:81) ~[flow-lit-template-24.5.8.jar:24.5.8]
	at org.vaadin.example.littemplateissue.Root2Layout.<init>(Root2Layout.java:28) ~[classes/:na]
	at org.vaadin.example.littemplateissue.Root2Screen.<init>(Root2Screen.java:6) ~[classes/:na]
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:501) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:485) ~[na:na]
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:195) ~[spring-beans-6.1.15.jar:6.1.15]
	... 89 common frames omitted

@mcollovati
Copy link
Collaborator

It looks like the issue could be related to the handling of the elementToMapTo thread local in Component class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🔖 Normal Priority (P2)
Development

No branches or pull requests

3 participants