【问题标题】:No qualifying bean of type found for dependency, vaadin没有为依赖项找到符合条件的 bean,vaadin
【发布时间】:2015-11-11 14:12:14
【问题描述】:

当我尝试从另一个配置文件注入参数时,没有找到类型为 [com.vaadin.ui.Horizo​​ntalLayout] 的合格 bean 的依赖项错误,如下所示: 主要配置:

@Configuration
@EnableVaadin
@Import(NavigationBar.class)
@ComponentScan("net.elenx")
public class SpringConfig {

    //Create whole view of MainView
    @Bean
    VerticalLayout template(@Qualifier("navigationBar") HorizontalLayout navigationBar) {
        VerticalLayout template = new VerticalLayout();
        //NavigationBar navigationBar = new NavigationBar();
        Sidebar sidebar = new Sidebar();
        template.setMargin(false);
        template.setSpacing(false);
        template.setHeight("100%");
        template.addComponent(navigationBar);
        template.addComponent(sidebar.getSidebar());
        template.setExpandRatio(sidebar.getSidebar(), 1.0f);
        return template;
    }
}

第二个配置:

@Configuration
@EnableVaadin
public class NavigationBar {

    @Bean
    HorizontalLayout navigationBar(Button hamburgerButton, Label elenxLogo) {
        System.out.println("Hello from NavigationBar bean!");
        HorizontalLayout navbar = new HorizontalLayout();
        navbar.setWidth("100%");
        navbar.setMargin(true);
        navbar.setHeight(50, Sizeable.Unit.PIXELS);
        navbar.addComponent(hamburgerButton);
        navbar.addComponent(elenxLogo);
        navbar.addStyleName("navigation-bar");
        return navbar;
    }

    @Bean
    Button hamburgerButton() {
        Button hamburgerButton = new Button();
        hamburgerButton.addStyleName("hamburger-button");
        hamburgerButton.setIcon(VaadinIcons.MENU);
        return hamburgerButton;
    }

    @Bean
    Label elenxLogo() {
        Label logo = new Label("ElenX");
        logo.addStyleName("elenx-logo");
        logo.setWidthUndefined();
        logo.setEnabled(false);
        return logo;
    }
}

那么实现这种注入的正确方法是什么?我希望每个元素都有 Bean,然后注入它们来构建整个布局。当我尝试更改此行时:

 @Bean
 VerticalLayout template(HorizontalLayout navigationBar) {

到这里:

 @Bean
 VerticalLayout template(@Qualifier("navigationBar") HorizontalLayout navigationBar) {

我收到“无法自动装配。限定符 bean 必须为‘组件’类型”错误。我对 Spring 很陌生,我不确定我做错了什么,Spring 不应该将我的 Horizo​​ntalLayout navigationBar 方法与 VerticalLayout 模板(Horizo​​ntalLayout navigationBar)的参数匹配吗?

【问题讨论】:

    标签: spring dependency-injection vaadin vaadin7 vaadin4spring


    【解决方案1】:

    您收到的错误消息告诉您,限定符 bean(这是用 @Qualifier("navigationBar") 注释的 HorizontalLayout)需要是 Spring @Component,即 HorizontalLayout 需要用 @Component 注释显然是不可能的。

    您的方法遇到的另一个问题是 Spring bean 默认是单例的。所以你最终会在你的应用程序中得到一个navigationBar 布局的实例。这当然是不够的,因为您需要为每个为用户创建的新 UI 对象创建一个新实例。 navigationBar Spring bean 需要有 prototype scope 以便应用程序上下文在每个注入事件上创建一个新实例。

    除此之外,我强烈建议您不要使用单独的 Vaadin UI 组件进行自动装配。 Spring 依赖注入机制并不打算在这个级别上使用。您应该使用更粗粒度的组件作为 Spring bean,例如整个 Vaadin views 或后端服务。构建单独的 UI 组件(如导航栏、表单或视图)应该在没有依赖注入的情况下完成。使用 DI 是多余的,因为您可以通过调用它们的构造函数来为常见的 UI 组件(例如导航栏、汉堡按钮或 elenx 徽标)创建实例。不需要在 UI 组件内部进行自动装配,这使得在这种情况下使用 Spring 容器完全是多余的。

    编辑: 在组件级别模块化 Vaadin 代码的最佳方法是使用类 CustomComponent 作为新组件的基类。您将构建一个名为 NavigationBar 的新 CustomComponent 子类,然后您可以像任何其他 Vaadin 组件一样实例化并添加到布局中。

    【讨论】:

    • 好的,我明白了,那么在 Vaadin 中编写侧边栏、导航栏等 UI 模块的最佳方法是什么?我关心最好的代码,谢谢你的回答!
    猜你喜欢
    • 2017-01-16
    • 2013-12-18
    • 1970-01-01
    • 2015-04-17
    • 2013-06-28
    • 2014-09-24
    • 2017-10-01
    • 2017-08-12
    相关资源
    最近更新 更多