You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Zeldor <pg...@gmail.com> on 2013/10/29 15:07:01 UTC

Guice and Wicket: using SessionScoped injections

I have a working Wicket [v6] application with Guice [v3] - I have used
dependency injection for repository operations right now and I want to
expend it into using services that are session scoped (one per user's
session). I have read through official documentation, various blog posts and
questions here, but I am not sure if I am using the correct approach.

I have two questions: 1. Do I use the correct way? 2. Do I need anything
special to run TestNG tests on classes that rely on SessionScoped
injections?

My setup: web.xml:

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>    
<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
    <listener-class>com.xxx.CustomServletConfig</listener-class>

MyApplication init:

@Override
protected void init()
{
    super.init();
    getResourceSettings().setResourcePollFrequency(null);
    getMarkupSettings().setStripWicketTags(true);
    getDebugSettings().setDevelopmentUtilitiesEnabled(true);
    GuiceComponentInjector injector = new GuiceComponentInjector(this, new
WebModule(), new GuiceModule());;
}

CustomServletConfig:

public class CustomServletConfig extends GuiceServletContextListener {

@Override
protected Injector getInjector() {
    return Guice.createInjector(new GuiceModule(), new WebModule());
}

WebModule:

public static class WebModule extends ServletModule {

    @Override
    protected void configureServlets() {
       
bind(WebApplication.class).toProvider(WicketGuiceAppProvider.class).asEagerSingleton();  

       
bind(IUserService.class).to(UserService.class).in(ServletScopes.SESSION);

        Map<String, String> params = new HashMap<String, String>();    
        params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");  

        filter("/*").through(WicketGuiceFilter.class, params);  
    }
}

In an example page I have:

@Inject
IUserService userService

...

userService.doSomething

At userService.doSomething during unit test I am getting Guice
OutOfScopeException, pointing to my bindings in ServletModule: Error in
custom provider, com.google.inject.OutOfScopeException?: Cannot access
scoped object. Either we are not currently inside an HTTP Servlet request,
or you may have forgotten to apply com.google.inject.servlet.GuiceFilter? as
a servlet filter for this request.

Is my configuration ok and I need to run unit tests differently (I am simply
launching my application with WicketTester), or is my design faulty?




--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Guice-and-Wicket-using-SessionScoped-injections-tp4662027.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Guice and Wicket: using SessionScoped injections

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

The configuration looks OK for normal deployment.
For testing it is not enough because WicketTester doesn't use web.xml and
thus GuiceFilter and CustomServletConfig are not used at all for the
configuration.

You will have to consult with Guice docs to see how to do this.
In a project I worked few years ago I had to do such "manual" configuration
for Guice Warp (Persistence) module. I guess something similar is needed
here too.


On Tue, Oct 29, 2013 at 4:07 PM, Zeldor <pg...@gmail.com> wrote:

>
> I have a working Wicket [v6] application with Guice [v3] - I have used
> dependency injection for repository operations right now and I want to
> expend it into using services that are session scoped (one per user's
> session). I have read through official documentation, various blog posts
> and
> questions here, but I am not sure if I am using the correct approach.
>
> I have two questions: 1. Do I use the correct way? 2. Do I need anything
> special to run TestNG tests on classes that rely on SessionScoped
> injections?
>
> My setup: web.xml:
>
> <filter>
>     <filter-name>guiceFilter</filter-name>
>     <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
> </filter>
> <filter-mapping>
>     <filter-name>guiceFilter</filter-name>
>     <url-pattern>/*</url-pattern>
> </filter-mapping>
> <listener>
>     <listener-class>com.xxx.CustomServletConfig</listener-class>
>
> MyApplication init:
>
> @Override
> protected void init()
> {
>     super.init();
>     getResourceSettings().setResourcePollFrequency(null);
>     getMarkupSettings().setStripWicketTags(true);
>     getDebugSettings().setDevelopmentUtilitiesEnabled(true);
>     GuiceComponentInjector injector = new GuiceComponentInjector(this, new
> WebModule(), new GuiceModule());;
> }
>
> CustomServletConfig:
>
> public class CustomServletConfig extends GuiceServletContextListener {
>
> @Override
> protected Injector getInjector() {
>     return Guice.createInjector(new GuiceModule(), new WebModule());
> }
>
> WebModule:
>
> public static class WebModule extends ServletModule {
>
>     @Override
>     protected void configureServlets() {
>
>
> bind(WebApplication.class).toProvider(WicketGuiceAppProvider.class).asEagerSingleton();
>
>
> bind(IUserService.class).to(UserService.class).in(ServletScopes.SESSION);
>
>         Map<String, String> params = new HashMap<String, String>();
>         params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
>
>         filter("/*").through(WicketGuiceFilter.class, params);
>     }
> }
>
> In an example page I have:
>
> @Inject
> IUserService userService
>
> ...
>
> userService.doSomething
>
> At userService.doSomething during unit test I am getting Guice
> OutOfScopeException, pointing to my bindings in ServletModule: Error in
> custom provider, com.google.inject.OutOfScopeException?: Cannot access
> scoped object. Either we are not currently inside an HTTP Servlet request,
> or you may have forgotten to apply com.google.inject.servlet.GuiceFilter?
> as
> a servlet filter for this request.
>
> Is my configuration ok and I need to run unit tests differently (I am
> simply
> launching my application with WicketTester), or is my design faulty?
>
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Guice-and-Wicket-using-SessionScoped-injections-tp4662027.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>