使用JavaConfig的Vaadin + Spring(没有SpringBoot)

时间:2015-08-21 01:10:10

标签: java spring vaadin

我正在尝试将vaadin与spring(没有spring-boot)和基于java注释的弹簧部分配置相结合。

自动装配似乎适用于vaadin-ui部分,但不适用于“custom-ui classes”(例如“public class LoginScreen extends CustomComponent”)。我在SysOut上获得了一个NPE或一个零对象。

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

XML-Configuration不是我喜欢的。

我正在关注本教程:Link

CustomConfiguration.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

@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

@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

@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) {
    }

}

Application.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” 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();
    }
}

我很感激一些帮助...

1 个答案:

答案 0 :(得分:2)

  

window.setContent( new LoginScreen());

如果您想要注入LoginScreen带注释的字段,Spring应该创建@Autowired

只需在LoginScreen

中注入Application个实例即可