带有 JavaConfig 的 Vaadin+Spring(没有 SpringBoot) [英] Vaadin+Spring (without SpringBoot) with JavaConfig

查看:32
本文介绍了带有 JavaConfig 的 Vaadin+Spring(没有 SpringBoot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 vaadin 与 spring(没有 spring-boot)和基于 java-annotation 的 spring 部分的配置结合起来.

i'm trying to combine vaadin with spring (without spring-boot) and java-annotation based configuration for the spring part.

自动装配似乎适用于 vaadin-ui 部分,但不适用于custom-ui classes"(例如public class LoginScreen extends CustomComponent").我在 SysOut 上收到 NPE 或空对象.

The autowiring seems to work on the vaadin-ui part but not in "custom-ui classes" (e.g. "public class LoginScreen extends CustomComponent"). I'm getting an NPE or a null-object on SysOut.

另外我注意到@ComponentScan(basePackages={"net.myapp"})"没有扫描bean.声明 bean 的唯一方法是在 CustomConfiguration 本身中.

Further i noticed that "@ComponentScan(basePackages={"net.myapp"})" is not scanning for beans. The only way to declare beans is in the CustomConfiguration itself.

XML 配置不是我喜欢的东西.

XML-Configuration is not something i prefer.

我正在关注本教程:链接

自定义配置.java

@Configuration
@ComponentScan(basePackages={"net.myapp"})
@EnableVaadin
public class CustomConfiguration {

// this is working but i want to use componentscan!
@Bean
public String test() {
    return "test...";
}

@Bean
public TestBean testBean() {
    return new TestBean();
}

@Bean
public LoginScreen loginScreenBean() {
    return new LoginScreen();
}

}

SpringVaadinServlet.java

SpringVaadinServlet.java

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = Application.class)
@SuppressWarnings("serial")
public class SpringVaadinServlet extends VaadinServlet implements SessionInitListener {

@Autowired
protected VaadinUIProvider applicationProvider;

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    AutowireCapableBeanFactory ctx = ((ApplicationContext) 
            getServletContext().getAttribute("applicationContext")).getAutowireCapableBeanFactory();
    ctx.autowireBean(this);
}

@Override
protected void servletInitialized() {
    getService().addSessionInitListener(this);
}

@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
    event.getSession().addUIProvider(applicationProvider);      
}

}

VaadinUIProvider.java

VaadinUIProvider.java

@SpringComponent
@SuppressWarnings("serial")
public class VaadinUIProvider extends UIProvider {

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
        return Application.class;
    }

    @Override
    public UI createInstance(UICreateEvent event) {
        UI instance = new Application();
        System.out.println("applicationContext is null? " + applicationContext);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(instance);
        return instance;
    }

}

SpringApplicationContextListener.java

SpringApplicationContextListener.java

@WebListener
public class SpringApplicationContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CustomConfiguration.class);
        sce.getServletContext().setAttribute("applicationContext", applicationContext);     
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }

}

应用程序.java

@Theme("mytheme1")
@SpringUI
@SuppressWarnings("serial")
public class Application extends UI {

    @Autowired
    private TestBean testBean;

    @Autowired
    private String test;

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        // working
        System.out.println("init testBean: " + testBean);
        System.out.println("init test: " + test);

        Window window = new Window();
        window.setContent(new LoginScreen());
        window.setClosable(false);
        window.setWidth("400px");
        window.setHeight("280px");
        window.setModal(true);
        window.setDraggable(false);
        window.setResizable(false);
        window.center();
        addWindow(window);
        setSizeFull();
    }

}

还有下面的custom-ui class"登录屏幕.java

And the following "custom-ui class" LoginScreen.java

@UIScope
@SuppressWarnings("serial")
public class LoginScreen extends CustomComponent {

    public static final String VIEW_NAME = "";

    final FormLayout layout = new FormLayout();

    TextField userName = new TextField();
    TextField passWord = new TextField();
    Button submit = new Button("Submit");

    @Autowired
    private TestBean testBean;

    @Autowired
    private String test;

    public LoginScreen() {
        userName.setCaption("Benutzername:");
        passWord.setCaption("Passwort:");

// not working (null)
        System.out.println("loginscreen test: " + testBean);
        System.out.println("loginscreen test: " + test);

        setSizeFull();
    }
}

希望得到一些帮助...

I'd appreciate some help...

推荐答案

window.setContent(new LoginScreen());

如果您希望 @Autowired 注释字段被注入,Spring 应该创建 LoginScreen.

Spring should create LoginScreen if you want that @Autowired annotated fields become injected.

只需在 Application 类中注入 LoginScreen 实例

Just inject the LoginScreen instance in your Application class

这篇关于带有 JavaConfig 的 Vaadin+Spring(没有 SpringBoot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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