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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:47:44+00:00 2026-05-29T10:47:44+00:00

I have a factory as below, public final class Application { private static IFoo

  • 0

I have a factory as below,

public final class Application {

    private static IFoo foo;

    public static IFoo getFoo(String bar)
    {
        // i need to inject bar to the constructor of Foo
        // obvious i have to do something, not sure what
        Injector injector = Guice.createInjector();
        logger = injector.getInstance(Foo.class);
        return logger;              
    }

}

This is the Foo definition:

class Foo
{
   Foo(String bar)
   {

   }

}

OK. I m not sure how I can pass this parameter to Foo constructor with Guice?

Any ideas?

  • 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-29T10:47:45+00:00Added an answer on May 29, 2026 at 10:47 am

    All the "Guice Constructor Parameter" answers seem to be incomplete in some way.
    Here is a complete solution, including usage and a visual:

    enter image description here

    interface FooInterface {
        String getFooName();
    }
    

    // Annotate the constructor and assisted parameters on the implementation class

    class Foo implements FooInterface {
        String bar;
    
        @Inject
        Foo(@Assisted String bar) {
            this.bar = bar;
        }
    
        // return the final name
        public String getFooName() {
            return this.bar;
        }
    
    }
    

    // Create a factory interface with a create() method that takes only the assisted parameters.

    // FooFactory interface doesn’t have an explicit implementation class (Guice Magic)

    interface FooFactory {
        Foo create(String bar);
    }
    

    // Bind that factory to a provider created by AssistedInject

    class BinderModule implements Module {
    
        public void configure(Binder binder) {
            binder.install(new FactoryModuleBuilder()
                    .implement(FooInterface.class, Foo.class)
                    .build(FooFactory.class));
        }
    }
    

    // Now use it:

    class FooAction {
        @Inject private FooFactory fooFactory;
    
        public String doFoo() {
            // Send bar details through the Factory, not the "injector"
            Foo f = fooFactory.create("This foo is named bar. How lovely!");
            return f.getFooName(); // "This foo is named bar. How lovely!"
        }
    }
    

    Lots of helps here: https://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/assistedinject/FactoryModuleBuilder.html

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

Sidebar

Related Questions

I have a factory that looks like below: public IFoo GetFoo(IFile file) { return
I have function as below . public static object getClassInstance(string key, object constructorParameter) {
I have the class. Class User { private $_name; private $_email; public static function
I have a factory that returns an interface FormatService : public class FormatServiceFactory {
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have a factory class that populates objects with data. I want to implementing
I have a factory that builds the objects with longest lifetime in my application.
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
So I have a factory class and I'm trying to work out what the
I have several static factory patterns in my PHP library. However, memory footprint is

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.