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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:44:40+00:00 2026-06-10T23:44:40+00:00

Using Guice, if I have @Inject @Named(light) Color light; I can use bind(Color.class).annotatedWith(Names.named(light)) .toInstance(new

  • 0

Using Guice, if I have

@Inject @Named("light")
Color light;

I can use

bind(Color.class).annotatedWith(Names.named("light"))
    .toInstance(new Color("white"));

to bind it to white color if Color constructor only requires color name.

But how can I do it if Color constructor also requires objects which are created by Guice? For example:

@Inject
public Color(ColorSet colorset, String colorName) {...}

where colorset is create at runtime by Guice and in not available in configure(){…}.

I would like to do it without:

  • creating a class/subclass for each color that I need to inject (like WhiteColor)
  • using reflection directly in my code
  • changing my classes to use ColorFactoy factory; factory.get("light"); instead of @Named("light") Color light;
  • 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-10T23:44:41+00:00Added an answer on June 10, 2026 at 11:44 pm

    The best way to do this is not with a factory but with @Provides methods. My company uses Guice very, very extensively, and requestInjection is always considered a bad idea because it can easily set up a very fragile graph of implicit dependencies.

    Here’s what it should look like:

    public class FooModule extends AbstractModule {
      protected void configure() {
        // do configuration
      }
    
      @Provides
      @Named("white")
      Color provideWhiteColor(ColorSet colorSet) {
        return colorSet.white(); // or whatever
      }
    
      @Provides
      @Named("black")
      Color provideBlackColor(ColorSet colorSet) {
        return colorSet.black(); // or whatever
      }
    
      // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I inject a property to a class I don't have access to
I just started to use Guice. When I am using Guice my application classes
I'm trying to use construction injection using both Jersey-injected @Context parameters and Guice-injected parameters.
I have just started using Google Guice with my Tomcat webapp, and have noticed
I have a problem using Gin. Here is a simple example. @GinModules(AppModule.class) public interface
i am using guice with java play framework. i have a simple command interface,
I'm developing a small web framework using Guice. I have a Router object that,
I'm using Tomcat 6.0.33 with Java 6. I have this servlet ... public class
I have some Guice binding code using generics that compiles and functions fine from
I have this web application that i have created using Wicket, Guice and JPA.

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.