2

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

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.

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-Configuration is not something i prefer.

I'm following this Tutorial: 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();
    }

}

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

1 Answer 1

2

window.setContent(new LoginScreen());

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

Just inject the LoginScreen instance in your Application class

2
  • with @Autowired private LoginScreen loginScreenBean; and window.setContent(loginScreenBean); the Beans in LoginScreen.java are still null
    – hehe
    Aug 21, 2015 at 20:32
  • @hehe Not really, you are logging in the ctor, injection occurs after construction. Aug 21, 2015 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.