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 7906189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:49:16+00:00 2026-06-03T10:49:16+00:00

I typically have this kind of code pattern in my GWT project: Menu errorMenu

  • 0

I typically have this kind of code pattern in my GWT project:

Menu errorMenu = new Menu(user, userController, -1);
Menu  searchMenu = new Menu(user, userController, 0);

errorView.setMenu(errorMenu);
searchView.setMenu(searchMenu);

How do I inject a Menu instance in the ErrorView and other “views” using Gin/Guice:

public ErrorView implements View {
 // Inject menu instance here
 private Menu menu;
}

Such that, I don’t have to manually create and set objects?

Also for the Menu class, how can I inject the “user” and “userController” objects so I don’t have to pass it on each Menu instance every time it is instantiated.

  • 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-06-03T10:49:18+00:00Added an answer on June 3, 2026 at 10:49 am

    With help of this tutorial http://code.google.com/p/google-gin/wiki/GinTutorial your problem does not look so difficult. There are several steps you should run for injecting menu instance to the View.

    1. Add @Inject annotation to the menu field.

      public ErrorView implements View {
      
        @Inject
        private Menu menu;
      }
      
      public SearchView implements View {
      
        @Inject
        private Menu menu;
      }
      

      But in this case menu fields will be null during View object initialization (in constructor). Therefore I prefer to add this field to the constructor parametres.

      public ErrorView implements View {
      
        private final Menu menu;
      
        @Inject
        public ErrorView(Menu menu) {
          this.menu = menu;
        }
      }
      
      public SearchView implements View {
      
        private final Menu menu;
      
        @Inject
        public SearchView(Menu menu) {
          this.menu = menu;
        }
      }
      

      Ofcourse it will not work in case of you have many other parameters in the constructor of ErrorView, because all of them need to be injected as well.

    2. Now we must make sure that GIN knows that menu field in ErrorView should be
      injected to new Menu(user, userController, -1) and another one in
      SearchView to – new Menu(user, userController, 0). We can do this
      by the several ways:

      • Add annotations @Named("searchMenu") and @Named("errorMenu") to your menu fields.

        public ErrorView implements View {
        
          @Inject
          @Named("errorMenu")
          private Menu menu;
        }
        

        or

        public ErrorView implements View {
        
          private final Menu menu;
        
          @Inject
          public ErrorView(@Named("errorMenu") Menu menu) {
            this.menu = menu;
          }
        } 
        

        In your GIN module you should provide the definition of this annotation.

            public class ApplicationGinModule extends AbstractGinModule {
        
              protected void configure() {
                bind(Menu.class).annotatedWith(Names.named("errorMenu")).to(DefaultErrorMenu.class);
                bind(Menu.class).annotatedWith(Names.named("searchMenu")).to(DefaultSearchMenu.class);
        
                //assume that User and UserController classes have default constructors  
                //otherwise you should provide correct injection depending on your business-logic
                bind(User.class).in(Singleton.class); 
                bind(UserController.class).in(Singleton.class);  
              }
            }
        
            public class DefaultErrorMenu extends Menu {
        
              @Inject
              public DefaultErrorMenu(User user, UserController userController) {
                super(user, userController, -1);
              }
            }
        
            public class DefaultSearchMenu extends Menu {
        
              @Inject
              public DefaultSearchMenu(User user, UserController userController) {
                super(user, userController, 0);
              }
            }
        
      • Create your own annotations @SearchMenu and @ErrorMenu to your menu fields and define them in your module.

        Annotation sample:

        @Retention(RetentionPolicy.RUNTIME)
        @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
        @BindingAnnotation
        public @interface ErrorMenu {
        }
        

        Usage:

        public ErrorView implements View {
        
          @Inject
          @ErrorMenu
          private Menu menu;
        }
        

        or

        public ErrorView implements View {
        
          private final Menu menu;
        
          @Inject
          public ErrorView(@ErrorMenu Menu menu) {
            this.menu = menu;
          }
        } 
        

        Then define annotation the way exactly how you defined @Named(“ErrorMenu”):

        bind(Menu.class).annotatedWith(ErrorMenu.class).to(DefaultErrorMenu.class);
        

    In some examples I make menu field final and remove setter to it, but if you really need mutable state for menu you can leave it unchanged.

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

Sidebar

Related Questions

On my pages I have a table with rows that typically look like this:
I have a project coming up to build an interface which allows a user
The typical Python threadpool will have a structure like this one: def run(self): while
Background to Question: Software for analysing blood glucose readings for diabetics, typically have something
With my database upgrade scripts, I typically just have one long script that makes
I have a string which typically is in the format : 0xFF . I'll
I have written a stored procedure that, yesterday, typically completed in under a second.
I have a series of images. Each one is typically (but not always) similar
I have a typical User model (username, password, name, etc). I want to allow
I have this method that receives an ID number and downloads an HTML website

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.