Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:29:27+00:00 2026-05-27T09:29:27+00:00

I have a very simple Vaadin application. I want to inject the main window

  • 0

I have a very simple Vaadin application. I want to inject the main window of my Vaadin app using Guice.

My problem is that my main window is injected, but none of my @Inject directives inside this main window component are processed.

The full code source of my use cas is available on a bitbucket repo

The Vaadin application class:

public class MyVaadinApplication extends Application {

    protected final Provider<MainWindow> mainWindowProvider;

    @Inject
    @Named("title")
    protected String title;

    @Inject
    public MyVaadinApplication(Provider<MainWindow> mainWindowProvider) {
        this.mainWindowProvider = mainWindowProvider;
    }

    @Override
    public void init() {
        System.out.println("MyVaadinApplication:: found value <"+title+"> for injected title");
        setMainWindow(this.mainWindowProvider.get());
    }
}

The MainWindow class:

public class MainWindow extends Window {

    @Inject
    @Named("title")
    protected String title;
    @Inject
    @Named("label")
    protected String label;
    private static int globalCounter = 0;
    private int localCounter;

    public MainWindow(String caption, ComponentContainer content) {
        super(caption, content);
        initUI();
    }

    public MainWindow(String caption) {
        super(caption);
        initUI();
    }

    public MainWindow() {
        super();
        initUI();
    }

    private void initUI() {
        localCounter = globalCounter;
        globalCounter++;
        this.setCaption(title);
        this.addComponent(new Button(label+" ("+localCounter+")"));
    }
}

The GuiceServletContextListener where the guice binding are defined:

public class GuicyServletConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {

        ServletModule module = new ServletModule() {

            @Override
            protected void configureServlets() {
                serve("/*").with(GuiceApplicationServlet.class);

                bind(Application.class).to(MyVaadinApplication.class).in(ServletScopes.SESSION);
                bind(MainWindow.class).in(ServletScopes.SESSION);
                bindConstant().annotatedWith(Names.named("title")).to("This is a guicy Vaadin Application");
                bindConstant().annotatedWith(Names.named("label")).to("Guice me!");
            }
        };

        Injector injector = Guice.createInjector(module);

        return injector;
    }
}

What happens is that the MainWindow is correctly injected, but the label and title strings are always null. Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:29:28+00:00Added an answer on May 27, 2026 at 9:29 am

    the solution is to inject anything that need to be injected in the MainWindow class using constructor instead of field injection. See the tag stackoverflow-working-with-constructor-injection on the hg repo.

    Samples of the source code

    public class MainWindow extends Window {
    
        protected String title;
        protected String label;
        protected LabelCaption labelCaption;
        private static int globalCounter = 0;
        private int localCounter;
    
        @Inject
        public MainWindow(@Named("title") String title, @Named("label") String label, LabelCaption labelCaption) {
            super();
            this.title = title;
            this.label = label;
            this.labelCaption = labelCaption;
            initUI();
        }
    
        private void initUI() {
            localCounter = globalCounter;
            globalCounter++;
            this.setCaption(title);
            String labelFromLabelCaption = "labelCaption is null";
            if (labelCaption != null) {
                labelFromLabelCaption = labelCaption.getCaption();
            }
            this.addComponent(new Button(label + "/" + labelFromLabelCaption + "/(" + localCounter + ")"));
        }
    }
    

    Does not know yet why field injection does not work in this case.

    The solution was injected to me thanks to this post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very simple application just in the body of Main method. I have
i have very simple problem. I need to create model, that represent element of
I have very simple problem that I can't solve. I need to do something
I have very simple query. I want to make sure that I don't have
I have very simple window where I have 2 buttons - one for cancel,
I have a very simple script that creates a user: <?php include 'mysqlserver.php'; session_start();
I have written very very simple console application which supports some command line options.
I have very simple query that calls a UDF which splits a field by
I have a very simple list of objects that represt people. Each object has
I have very simple XML in a string that I'm trying to load via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.