Home > OS >  Spring beans are not being injected into the Vaadin view
Spring beans are not being injected into the Vaadin view

Time:02-02

I've downloaded a Vaadin Flow starter and tried to use it but for some reason, beans were not being injected:

@PageTitle("About")
@Route(value = "about", layout = MainLayout.class)
public class AboutView extends VerticalLayout {
    
    @Autowired
    private UserService userService;
    
    public AboutView() {
        ...
    }
}

The userService remained null.

I still can pass beans via the constructor:

@PageTitle("About")
@Route(value = "about", layout = MainLayout.class)
public class AboutView extends VerticalLayout {
    public AboutView(UserService userService) {
        ...
    } 
}

But it's not always convenient.

I created a REST service in the same project and the injection worked like a charm even without the annotation:

@RestController
@AllArgsConstructor
@RequestMapping("user")
public class UserController {

    private UserService userService;    
    ...
}

What is the issue with Vaadin Flow? I'm using Vaadin Flow 24.0.0.alpha8, SpringBoot 3.0.1, and Java 19.

P.S. By the way, I also tried Vaadin CDI with PayraMicro and there was the same problem. @EJB and @Inject annotations didn't work.

CodePudding user response:

Fieeld injection happens after the constructor has run. Use a @PostConstruct annotated method to access fields

  • Related