Vaadin视图中的自动装配服务和组件不起作用 [英] Autowiring services in Vaadin view and components not working

查看:83
本文介绍了Vaadin视图中的自动装配服务和组件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员们!我对Vaadin来说还比较陌生,所以请饶恕我。
我正尝试将服务层自动连接到我的视图中,如下所示:

Hello fellow programmers! I am relatively new to Vaadin so spare me please. I am trying to autowire my service layer into my view as follows:

@Route("")
@PWA(name = "First time bruh", shortName = "Project Base")
public class MainView extends VerticalLayout {

    private TextField filterText = new TextField();
    private Grid<Customer> grid = new Grid<>(Customer.class);
    private CustomerForm customerForm = new CustomerForm(this);

    @Autowired
    private CustomerService customerService;

并且customerService依赖项注入正常工作,但是当我尝试在组件中使用它时,它返回null :

and the customerService dependency injections works properly, however when i try to use it in a component it returns null:

@Route
public class CustomerForm extends FormLayout {

    @Autowired
    private CustomerService customerService;

我尝试使用@Component和@SpringComponent注释类,但是依赖项注入不起作用,并且我认为问题不是出自该类不是bean的事实,因为MainView类也不是bean。

I've tried annotating the class with @Component and @SpringComponent but the dependency injection does not work and i think that the problem does not come from the fact that the class is not a bean, because MainView Class is also not a bean.

我的愿望是自定义子组件我创建的可以访问服务层。

My wish is the custom sub components that i create to have access to the service layer.

在此先感谢您的帮助!

推荐答案

在Vaadin UI中,您只能在路由端点(具有 @Route 批注的视图)中注入,并且仅当该视图由导航到注释中指定的路线。 (因为只有这样,该视图的实例化才会自动完成。)

In Vaadin UI, you can inject only in route endpoints (views that have the @Route annotation), and only if that view is opened by navigating to the route specified in the annotation. (Because only then then instantiation of that view is done "automatically").

根据经验:每当您使用实例化某些对象时, new 关键字,注入/自动装配无效。

As a rule of thumb: Whenever you instantiate something yourself using the new keyword, injection/autowiring does not work.

我对您的情况了解是:

MainView ,在其中您要添加 CustomerForm

What I understand of your situation is:
You have a MainView, within that you want to add a CustomerForm.

以下是实现该目标的方法:

CustomerService 注入到 MainView ,并将 CustomerService 实例传递到 CustomerForm 的构造函数中

Here is how to achieve that:
Inject the CustomerService into the MainView, and pass the CustomerService instance into the constructor of CustomerForm

@Route
public class MainView extends VerticalLayout {
    public MainView(CustomerService customerService) { // customerService will be injected
        CustomerForm customerForm = new CustomerForm(customerService);
        add(customerForm);
    }
}



public class CustomerForm extends FormLayout {
    public CustomerForm (CustomerService customerService){
        ...
    }
}

另一种方法是使 CustomerForm a @Component (一定要记得在spring配置类中对其进行了正确的扫描),将服务注入其中,然后将整个表单注入到 MainView

Another approach would be to make the CustomerForm a@Component (do remember to scan it properly in the spring configuration class), inject the service into it, and then inject the whole form into the MainView:

@Route
public class MainView extends VerticalLayout {
    public MainView(CustomerForm customerForm) {  // customerForm will be injected
        add(customerForm);
    }
}



@Component
public class CustomerForm extends FormLayout {
    public CustomerForm (CustomerService customerService){ // customerService will be injected
        ...
    }
}

这篇关于Vaadin视图中的自动装配服务和组件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆