Spring/Vaadin: Creating the application context

Hi there,
I’m trying to get a first grip on Spring + Vaadin, using the example project on :
http://vaadin.github.io/spring-tutorial/

Especially, I’m looking at the one without Spring Boot. I have the example running, now I’d like to integrate parts like a db etc. (for which I want to use Spring configuration, annotation-based, autowired) The problem I have is that I don’t see how to populate the Spring Appplication context (using annotation methods only). There are several hints in the github project, but to be honest: I’m lost.

Haven’t read the tutorial but generally in Spring to create beans you need to create a @Configuration where you create the beans.

Here is a short example:

[code]
@Lazy
@Configuration
@ComponentScan(“your.base.package”)
class MyBeans {

@Bean
public MyBean myBean() {
// Create the bean in some way here
}
[/code]Spring will scan the classpath for @Configuration annotated class and load the beans from there.

Thanks for the quick response, Yes, I know, that is the general way to it, and I tried so (I’ve build Spring apps before), but any beans created this way, end up in null pointers if autowired, so something is missing, it looks like the @Configuration annotatted class is not being picked up by spring at all.

My class is annotated with:
@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableJpaRepositories

Perhaps you can checkout this example: https://github.com/vaadin/framework8-demo/tree/master/spring-demo

Spring boot with Vaadin 8 and Jpa repository

I’ll try to, thanks. I’d rather have an example without SpringBoot though. This github project seems not be clonable, I’ll make a plain copy of it.

I see it’s part of a cloable project, I’ll clone the parent

This is a SpringBoot application, in which no beans have been configured, at least not in the traditional way with @Bean.
So my problem is still that I do not see the relationship between the way Vaadin implemented (Spring)VaadinServlet and the way context-initialisation is described in Spring in Action 4th Ed. p 135/136. In there, the (web) application is bootstrapped by implementing AbstractAnnotationConfigDispatcherServletInitializer. The implementaion populates two Spring-contexts, the web-context by implementing getServletConfigClasses and the application-context, by implementing
getRootConfigClasses.
I’m looking for the equivalent of that in the Vaadin/Spring implementation example (without using SpringBoot). I think that is the missing link why my @Configuration class is not picked up by Spring. Thanks for any suggestions.

Let me try to re-phrase the question: I would like to add somthing like this:

[font=courier new]
@Configuration
public class MyConfiguration {
@Bean Bla getBla() {
return new Bla();
}
}

So that I can use the bean like this:
@Autowired
private Bla bla;
[/font]

This seems pretty plain usage of Spring to me, but I cann’t getr it working in the Vaadin/Spring example.

The auowired property remains null.

And you have set up the WebApplicationInitializer as described in
http://vaadin.github.io/spring-tutorial/#_using_a_webapplicationinitializer
?

The important part there is AnnotationConfigWebApplicationContext that will enable classpath scanning for the configurations.

Hi again,
No I had not yet done that, I just don’t see what is required and what is ‘just a suggestion’, would this WebContexInitializer class replace the code in MyUi :
@WebServlet(urlPatterns = “/*”, name = “MyUIServlet”, asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
and, after adding such a class, to which class should applicationContext.xml refer after this change? (or even more: would I still need applicationContext.xml at all?)
Would the new class be additional: I am really lost here,
The whole purpose of the example Spring-Vaadin integration should be the ability to use Spring beans I guess,
I’d indeed be happy if I could configure any @Beans and use them @Autowired, but have not managed to get at that point after spending a day or two.
In the debugger, I can see that Spring creates a bean in the factory, but the wiring is not done at all…
I’m in a situation where I have a few colleagues who do know Vaadin, but don’t know Spring yet. I happen to know the basics of Spring, but not Vaadin yet.
We need to close that gap somehow.

Thanks in advance,
(BTW: I’ll be back June 26)

^^^ This. ^^^ Why hasn’t this been addressed in 3 years? I’m about to throw in the towel using Vaadin because like Joris explains, it seems impossible to @Autowire Beans into Vaadin Components annotated with @Route. I can’t even grab the ApplicationContext in a trivial project.

@Route(value = "content", layout = MainView.class)
public class MyLayout extends VerticalLayout implements RouterLayout {

  @Autowired private ApplicationContext context; // Always null
  
   public MyLayout() {
    comps.add(context.getBean(MyComponentA.class)); // context always null
	}

Turns out that the solution is:

public MyLayout() {
    comps.add(context.getBean(MyComponentA.class)); // context always null
	}

to:

	@PostConstruct
	private void init() {
    comps.add(context.getBean(MyComponentA.class));
	}

Hi,

I’m sorry that you spent time on this error.
You can’t use spring injected field in the constructor because spring will inject it later.
That’s why it’s available in PostConstruct.
Spring recommends to use constructor injection instead of field injection: https://spring.io/blog/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required

You also have an example here: https://vaadin.com/docs/v14/flow/spring/tutorial-spring-routing.html