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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:39:25+00:00 2026-05-28T03:39:25+00:00

After reading through many code examples for Google Guice, I see a lot of

  • 0

After reading through many code examples for Google Guice, I see a lot of code that looks like this:

public class MyModule implements Module {
    public void configure(Binder binder) {
        binder.bind(Service.class).to(ServiceImpl.class);
    }
}

public class Widget {
    private Injector injector = Guice.createInjector(new MyModule());

    public void doSomething() {
        Service service = injector.getInstance(Service.class);
        service.run();
    }
}

It is my understanding that code which explicitly calls the IoC mechanism (such as injector.getInstance(Class<T>) in Guice or appContext.getBean(String) in Spring) is indeed an IoC anti-pattern.

In these examples I notice there is no mention of the @Inject or @ImplementedBy annotations, which I believe are Guice’s workarounds to this anti-pattern.

How could I rewrite the code snippet above so that I’m not manually/explicitly invoking the injector? I’d like everything configured so that by the time execution gets to the doSomething() method, the application already “knows” what Service implementation we’ll be using…

Thanks in advance!

  • 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-28T03:39:25+00:00Added an answer on May 28, 2026 at 3:39 am

    Make Guice aware of your Widget by adding it to a module and injecting it when needed using annotations.

    You will need to add Service to its constructor:

    public class Widget {
    
        private final Service service;
    
        @Inject
        public Widget(Service service) {
            this.service = service;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this description of late static binding (LSB) I see pretty clearly what
After reading through many of the questions here about DB schema migration and versions,
I am trying to learn ASP.NET MVC. After reading through many articles on the
I've spent the morning reading through page after page of StackOverflow posts about this
i was reading through the sample code ListAdder, and there are many asserts right
After reading this question , I was reminded of when I was taught Java
After reading What’s your/a good limit for cyclomatic complexity? , I realize many of
After reading the answers to the question Calculate Code Metrics I installed the tool
After reading this answer: best way to pick a random subset from a collection?
I am new to android and after going through many tutorials I still can't

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.