Spring integration - @Route as bean

I don’t understand, using spring integration (without Spring Boot) when a @Route is created which kind of Spring bean is and its scope.
I tried to list all beans in my spring context and no bean corresponds to my @Route annotated class.
Tks
Tullio

Hi! … there is an interesting discussion about this topic [in the comments section in the official documentation]
(https://vaadin.com/docs/v14/flow/spring/tutorial-spring-routing.html#replies17975251).
Taken from there: In case the route is not an explicit Spring component the instance is created via ApplicationContext::getAutowireCapableBeanFactory::createBean. This doesn’t make it managed but only allows to use injections/autowiring.

This is a quite dramatic breach of the previous Vaadin 8 Spring integration.
The problem is that is not true the autowiring is working because session bean, like some services in my environment, are not seen by any @Route.
This break all architecture I created.
Any suggestion ?
Tks

You should still be able to autowire session scoped bean to your view.

I agree, but when I do this I get a spring error : “session” is non active in the therad (or something like that).
Is it a bug ?
My @Route is annotated as @UIScope
Tks

Can you supply a short example reproducing the problem? … if not, can you attach a full stacktrace so I can take a look?

If you’re getting “Scope ‘session’ is not active for the current thread; IllegalStateException: No thread-bound request found” then maybe you’re getting a similar problem than [this one]
(https://stackoverflow.com/questions/21286675/scope-session-is-not-active-for-the-current-thread-illegalstateexception-no). As explained there: “The singleton is available anywhere, it is ok. However session/request scope is not available outside a thread that is attached to a request.”

Regards.

Session beans are not accessible only from @Route.
Here the stack trace.
HomePage is the name of my @Route, funzioneMenuServiceImpl is a session bean.
Tks

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘homePage’: Unsatisfied dependency expressed through field ‘funzioneMenuService’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘funzioneMenuServiceImpl’: Scope ‘session’ is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$1(AbstractBeanFactory.java:359)
at com.vaadin.flow.spring.scopes.BeanStore.doGet(BeanStore.java:127)
at com.vaadin.flow.spring.scopes.BeanStore.lambda$get$0(BeanStore.java:71)
at com.vaadin.flow.spring.scopes.BeanStore.execute(BeanStore.java:135)
at com.vaadin.flow.spring.scopes.BeanStore.get(BeanStore.java:71)
at com.vaadin.flow.spring.scopes.AbstractScope.get(AbstractScope.java:44)
at com.vaadin.flow.spring.scopes.VaadinUIScope.get(VaadinUIScope.java:42)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:356)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:227)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1155)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:416)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:349)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
at com.vaadin.flow.spring.SpringInstantiator.getOrCreate(SpringInstantiator.java:106)
at com.vaadin.flow.di.Instantiator.createRouteTarget(Instantiator.java:158)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$getRouteTarget$1(AbstractNavigationStateRenderer.java:127)
at java.util.Optional.orElseGet(Optional.java:267)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.getRouteTarget(AbstractNavigationStateRenderer.java:126)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.createChain(AbstractNavigationStateRenderer.java:314)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:195)
at com.vaadin.flow.router.Router.handleNavigation(Router.java:223)
at com.vaadin.flow.router.Router.navigate(Router.java:194)
at com.vaadin.flow.router.Router.initializeUI(Router.java:92)
at com.vaadin.flow.server.BootstrapHandler.createAndInitUI(BootstrapHandler.java:1489)
at com.vaadin.flow.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:459)
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540)
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:247)
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘funzioneMenuServiceImpl’: Scope ‘session’ is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:368)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
… 58 common frames omitted
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.request.SessionScope.get(SessionScope.java:55)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:356)
… 63 common frames omitted

However session/request scope is not available outside a thread that is attached to a request`

The problem is @Route is attached to a request ?
I believe it should be but I’m not sure,
Tks

I did a following bean

@Component
@SessionScope
public class TestBean {
	int value = 10;
	
	public int getValue() {
		return value;
	}
}

And the I use it in MainLayout

...
@CssImport("./styles/shared-styles.css")
public class MainLayout extends AppLayout {

	TestBean testBean;
	
    public MainLayout(TestBean testBean) {
    	this.testBean = testBean;
        createHeader();
        createDrawer();
    }

    private void createHeader() {
        H1 logo = new H1("Vaadin CRM "+testBean.getValue());
        logo.addClassName("logo");
		...

And it seems to work properly, what we do differently?

I tested you code and it worked, the I tried to find the difference and the difference was :

  • You annotated as @SessionScope
  • I annotated as @Scope(“session”)

It’s quite stupid but Your notation is accepted my notation isn’t.
As from Spring documentation there’s no difference but…
Tks anyway

I was too much optimistic.
At first level it find session bean if a session bean autowires other session bean with two or three layer of nesting again it goes wrong.
Here my test stack trace.
Tks

java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.holonplatform.vaadin.flow.navigator.internal.listeners.AbstractNavigationTargetListener.invokeMethod(AbstractNavigationTargetListener.java:78)
at com.holonplatform.vaadin.flow.navigator.internal.listeners.DefaultNavigationTargetAfterNavigationListener.lambda$afterNavigation$2(DefaultNavigationTargetAfterNavigationListener.java:75)
at java.lang.Iterable.forEach(Iterable.java:75)
at com.holonplatform.vaadin.flow.navigator.internal.listeners.DefaultNavigationTargetAfterNavigationListener.afterNavigation(DefaultNavigationTargetAfterNavigationListener.java:72)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$fireAfterNavigationListeners$2(AbstractNavigationStateRenderer.java:340)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.fireAfterNavigationListeners(AbstractNavigationStateRenderer.java:340)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:254)
at com.vaadin.flow.router.Router.handleNavigation(Router.java:223)
at com.vaadin.flow.router.Router.navigate(Router.java:194)
at com.vaadin.flow.router.Router.initializeUI(Router.java:92)
at com.vaadin.flow.server.BootstrapHandler.createAndInitUI(BootstrapHandler.java:1489)
at com.vaadin.flow.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:459)
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540)
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:247)
at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:95)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘scopedTarget.menuBuilderServiceImpl’: Scope ‘session’ is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:368)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at axioma.web.template.services.funzioni.MenuBuilderServiceImpl$$EnhancerBySpringCGLIB$$c229188f.buildMenu()
at axioma.web.template.ui.pages.CasaPage.onShow(CasaPage.java:53)
… 44 common frames omitted
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.request.SessionScope.get(SessionScope.java:55)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:356)
… 49 common frames omitted