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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:32:06+00:00 2026-05-14T04:32:06+00:00

I am trying to migrate a small project, replacing some factories with Guice (it

  • 0

I am trying to migrate a small project, replacing some factories with Guice (it is my first Guice trial). However, I am stuck when trying to inject generics. I managed to extract a small toy example with two classes and a module:

import com.google.inject.Inject;

public class Console<T> {
  private final StringOutput<T> out;
  @Inject
  public Console(StringOutput<T> out) {
    this.out = out;
  }
  public void print(T t) {
    System.out.println(out.converter(t));
  }
}

public class StringOutput<T> {
  public String converter(T t) {
    return t.toString();
  }
}

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;


public class MyModule extends AbstractModule {

  @Override
  protected void configure() {
    bind(StringOutput.class);
    bind(Console.class);
  }

  public static void main(String[] args) {
    Injector injector = Guice.createInjector( new MyModule() );
    StringOutput<Integer> out = injector.getInstance(StringOutput.class);
    System.out.println( out.converter(12) );
    Console<Double> cons = injector.getInstance(Console.class);
    cons.print(123.0);
  }

}

When I run this example, all I got is:

Exception in thread “main” com.google.inject.CreationException: Guice creation errors:

1) playground.StringOutput<T> cannot be used as a key; It is not fully specified.
  at playground.MyModule.configure(MyModule.java:15)

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:354)
    at com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:152)
    at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
    at com.google.inject.Guice.createInjector(Guice.java:92)

I tried looking for the error message, but without finding any useful hints. Further on the Guice FAQ I stumble upon a question about how to inject generics. I tried to add the following binding in the configure method:

bind(new TypeLiteral<StringOutput<Double>>() {}).toInstance(new StringOutput<Double>());

But without success (same error message).

Can someone explain me the error message and provide me some tips ? 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-14T04:32:06+00:00Added an answer on May 14, 2026 at 4:32 am

    I think the specific issue you’re seeing is probably because of the bind(Console.class) statement. It should use a TypeLiteral as well. Or, you could just bind neither of those and JIT bindings will take care of it for you since both of the types involved here are concrete classes.

    Additionally, you should retrieve the Console with:

    Console<Double> cons = 
       injector.getInstance(Key.get(new TypeLiteral<Console<Double>>(){}));
    

    Edit: You don’t need to bind to an instance just because you’re using a TypeLiteral. You can just do:

    bind(new TypeLiteral<Console<Double>>(){});
    

    Of course, like I said above you could just skip that in this case and retrieve the Console from the injector using a Key based on the TypeLiteral and the binding would be implicit.

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

Sidebar

Related Questions

I'm trying to migrate a small SVN repository w/ 2000+ revs with 10+ branches
I want to migrate to Kohana with my small websites and I'm trying to
I'm trying to migrate some models ImageFields to using the S3BotoStorage storage backend from
Im trying to migrate an old project from Delphi 5 to Delphi XE. When
We're trying to migrate from current Ant build to Maven. In the current project,
I am trying to migrate a small ASP.NET 'web site' (not web application) to
I'm trying to migrate an existing eclipse web project (wtp) to maven, using m2e.
Trying to migrate a Richfaces 3.3 project to 4. Got to a rich:suggestionBox component
Intro : I'm trying to migrate our Trac SQLite to a PostgreSQL backend, to
I'm trying to migrate a website from Windows 2000 to Win2k3 using the IIS

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.