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

  • Home
  • SEARCH
  • 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 7525255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:29:31+00:00 2026-05-30T03:29:31+00:00

I am trying to learn Guice and I have a question on how to

  • 0

I am trying to learn Guice and I have a question on how to correctly wire up things. I am trying to get the call in BoofPanels doSomething() method to automatically use the MockFooImpl, but I don’t think things are wired properly.

In my case I have:

@ImplementedBy(MockFooImpl.class)
public interface FooInterface {
    public int getBar(String one, String two);
}


@Singleton
public final class MockFooImpl implements FooInterface {
    @Inject
    public MockFooImpl() {
    }

    @Override
    public int getBar(String one, String two) {
      return 1;
    }
}


public class Baz extends JFrame  {

   private BoofPanel boofPanel;

   public Baz(String one, String two, Injector injector){
        // Constructor with args
        boofPanel = new BoofPanel("aString", 565);
        injector.injectMembers(boofPanel);
   }

   public static void main(String[] args){
       final Injector injector = Guice.createInjector();

       java.awt.EventQueue.invokeLater(new Runnable() {  
           @Override
           public void run() {
               final Baz baz = new Baz("one","two", injector);
               baz.setVisible(true);
           }
       });
   }
}


public class BoofPanel extends JPanel {
    @Inject
    private FooInterface fooI;

    public BoofPanel(String aString, int anInt){

    }
    public void doSomething(){
       fooI.getBar();
    }
}

Any help would be appreciated.

Thanks!

  • 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-30T03:29:32+00:00Added an answer on May 30, 2026 at 3:29 am

    Guice isn’t magic. It will only inject into an object if either it creates it, or you ask it to. If you’re creating your own BoofPanel, then you’ll need to ask Guice to inject it. Add a constructor to Baz that looks like:

    public Baz(Injector injector) {
        boofPanel = new BoofPanel(whatever, the, params, are);
        injector.injectMembers(boofPanel);
    }
    

    The BoofPanel constructor doesn’t need to do anything to the fooI variable; it can leave it null. You’ll also need to make main’s injector variable final, so the anonymous class created in main can capture it and pass it into the Baz constructor.

    If you’d rather have BoofPanel created by Guice, then you’ll need to supply all the constructor parameters it needs to Guice, and call injectMembers on Baz itself.

    EDIT: Specific changes to the code above, after edits.

    1. Add a final to this declaration, to make it usable inside the anonymous inner class:

      final Injector injector = Guice.createInjector();

    2. Remove this line, as it’s unnecessary:

      injector.getInstance(MockFooImpl.class);

    3. Add the injector to the invocation of the Baz constructor, to get it to where BoofPanel is created (i’ve also removed the unnecessary final here):

      Baz baz = new Baz("one", "two", injector);

    4. Add the injector to the declaration of the Baz constructor, to receive it from the main method:

      public Baz(String one, String two, Injector injector) {

    5. Add an injection into boofPanel to the Baz constructor, right after the new BoofPanel line, to get Guice to inject BoofPanel:

      injector.injectMembers(boofPanel);

    That’s it.

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

Sidebar

Related Questions

I am trying learn the use of lambda expressions and hence still struggling to
Trying to learn about php's arrays today. I have a set of arrays like
Im trying to learn a bit about c++ and have run in to some
I'm trying to learn python and have encountered some strange behaviour. I am experimenting
I have been trying to get a basic reorderlist working following this guide ->
I am trying to learn Django (and Python) and have created my own app
I'm trying to learn the framework kohana. I have defined a new controller under
I have been trying to get a good grasp of Azure Table storage for
I am somewhat new to jQuery but have been having fun trying learn about
Trying to learn shaders and get a dead simple directional light source working but

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.