But you don't have to implement it twice. You just have to bind it
twice. I just implemented this to make sure I wasn't selling a lie,
but here what I got:
public class MyServletModule extends ServletModule
{
@Override
protected void configureServlets()
{
bind(AServlet.class).annotatedWith(Names.named("a")).to(AServlet.class).in(Singleton.class);
bind(AServlet.class).annotatedWith(Names.named("b")).to(AServlet.class).in(Singleton.class);
serve("/a").with(Key.get(AServlet.class, Names.named("a")),
Collections.singletonMap("PROPERTY", "a"));
serve("/b").with(Key.get(AServlet.class, Names.named("b")),
Collections.singletonMap("PROPERTY", "b"));
}
}
public class AServlet extends HttpServlet
{
@Override
public void init(final ServletConfig config) throws ServletException
{
System.out.println("Initing me! " + this.toString());
System.out.println(config.getInitParameter("PROPERTY"));
}
}
And here's the output from starting up the servlet container:
Initing me! com.example.servlets.AServlet@23a82e92
a
Initing me! com.example.servlets.AServlet@3e9d9edd
b
The key is to not bind AServlet.class as a singleton, but bind
Key.get(AServlet.class, Names.named("a")) and Key.get(AServlet.class,
Names.named("b")) as singletons.
Hope that helps,
Jared
On Tue 02 Oct 2012 05:41:19 AM CDT, Andreas Lüdeke wrote:
> Hi,
>
> with this approach i still need to Implement AServlet twice (e.g.
> AServletImpl, BServletImpl extends AServlet) and need to annotate it
> with @Named('a') and @Named('b').
>
> This is exactly what i would like to avoid.
>
> On Tuesday, October 2, 2012 6:50:26 AM UTC+2, Jared Bunting wrote:
>
> I haven't done this, but can't you do something like this?
>
> serve("/a").with(Key.get(AServlet.class, Names.named("a")),
> Collections.singletonMap('PROPERTY', "a"));
> serve("/b").with(Key.get(AServlet.class, Names.named("b")),
> Collections.singletonMap('PROPERTY', "b"));
>
> Just make sure you don't bind AServlet as a singleton.
>
> -Jared
>
> On Mon, Oct 1, 2012 at 3:11 PM, Andreas Lüdeke <[email protected]
> <javascript:>> wrote:
>
> Hi,
>
> i would like to know whether it is possible to register the
> same Servlet class in guice with two different configurations.
> The goal is to have 2 different instances of this class at
> runtime.
>
> something like:
>
> serve("/a").with(AServlet.class,
> Collections.singletonMap('PROPERTY', "a"));
> serve("/b").with(AServlet.class,
> Collections.singletonMap('PROPERTY', "b"));
>
> Thx
>
> Andreas
>
> --
> You received this message because you are subscribed to the
> Google Groups "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/5nL_SWqDAIkJ
> <https://groups.google.com/d/msg/google-guice/-/5nL_SWqDAIkJ>.
> To post to this group, send email to
> [email protected] <javascript:>.
> To unsubscribe from this group, send email to
> [email protected] <javascript:>.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en
> <http://groups.google.com/group/google-guice?hl=en>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/rw81HchFWYsJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.