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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:55:55+00:00 2026-05-28T19:55:55+00:00

I am attempting to inject an annotated variable into the REQUEST scope: Map<Key<?>, Object>

  • 0

I am attempting to inject an annotated variable into the REQUEST scope:

Map<Key<?>, Object> seedMap = ImmutableMap.<Key<?>, Object>builder().
  put(Key.get(String.class, Names.named("name")), name).build();
return ServletScopes.scopeRequest(new InjectingCallable<>(injector, 
  GetModule.class), seedMap).call();

Where, InjectingCallable injects GetModule inside the REQUEST scope:

/**
 * A Callable that is constructed in one scope and injects a Callable into a potentially separate
 * scope.
 * <p/>
 * @param <V> the type of object returned by the Callable
 * @author Gili Tzabari
 */
public final class InjectingCallable<V> implements Callable<V>
{
    private final Injector injector;
    private final Class<? extends Callable<V>> delegate;

    /**
     * Creates a new InjectingCallable.
     * <p/>
     * @param injector the Guice injector
     * @param delegate the class to inject and delegate to
     */
    public InjectingCallable(Injector injector, Class<? extends Callable<V>> delegate)
    {
        Preconditions.checkNotNull(injector, "injector may not be null");
        Preconditions.checkNotNull(delegate, "delegate may not be null");

        this.injector = injector;
        this.delegate = delegate;
    }

    @Override
    public V call() throws Exception
    {
        return injector.getInstance(delegate).call();
    }
}

GetModule is defined as follows:

@RequestScoped
private static class GetModule implements Callable<Module>
{
    private final String name;
    private final Session session;

    @Inject
    public GetModule(@Named("name") String name, Session session)
    {
        this.name = name;
        this.session = session;
    }
}

When I run this code I get this error:

1) No implementation for java.lang.String annotated with @com.google.inject.name.Named(value=name) was bound.
  while locating java.lang.String annotated with @com.google.inject.name.Named(value=name)

If I bind the same variable to the global scope it works. If I remove the annotation, it works. This problem seems to be specific to Request-scoped annotated variables. 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-28T19:55:56+00:00Added an answer on May 28, 2026 at 7:55 pm

    The problem is that you don’t have a binding for this type. Just because you are explicitly seeding the value does not mean you don’t have to bind it. you could say:

    bind(String.class)
        .annotatedWith(Names.named("name"))
        .toProvider(Providers.<String>of(null));
    

    and then if the name variable has the value "foo", you will get "foo" injected because you’re seeding it. Seeding a value places it in the scope (which is just a cache) so that Guice won’t run the provider of the value. By using a provider of null you can just let the value blow up if it’s not seeded.

    In short, Guice requires that you specify a way to provision every dependency, regardless of whether you plan to manually seed scopes (which should be a fairly rare thing btw).

    Some unsolicited advice:
    – Please avoid injecting the injector. It makes catching these kinds of problems harder. It’s best to have a single “root object”. This is the single object you need to call injector.getInstance to create. For a lot of applications, this can just be your application server. (e.g. – injector.getInstance(MyServer.class).startServer()). Why does this help you? It makes it easier to detect at startup that all your dependencies are satisfied. If you inject the injector during a request and can call it to create arbitrary objects, you run the risk of getting some provision error due to a missing binding much later during runtime. Also if you do all your getInstance calls early on, it’s easier to write tests that do this for you so that you can simply run a test to know that your Guice bindings are satisfied.

    UPDATE:

    If I bind the same variable to the global scope it works.

    Hmm, did you basically do what I did? If so, my explanation above explains why that works :-).

    If I remove the annotation, it works.

    The reason this works is because Guice does have a binding for String since String has an empty constructor :-). Basically you have to have a single @Inject-able constructor, a no-arg constructor, or a provider to bind a type.

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

Sidebar

Related Questions

I am attempting to inject an object into my saga. Using the following endpoint,
I am receiving the following error when attempting to inject my dao into a
I'm attempting to use Ninject to inject repositories into controllers of my MVC project.
Attempting to render a model object into a JSON structure via a partial, like
Attempting to insert an escape character into a table results in a warning. For
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
When attempting to compile my C# project, I get the following error: 'C:\Documents and
I am maintaining an ASP.NET site, and I was attempting to get the dialogs
Attempting to run a MIB based SNMP script and I running into an issue.
Trying to integrate Spring 2.5.5 with Xfire 1.2.6, I'm attempting to inject one of

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.