I'm not sure this will help - but thought I'd post how I'm configuring
Guice. I've *not* had a serialization problem in my pages.

There are several ways to configure Guice and Wicket, I am configuring Guice
as a Module. I've tried to build up the example from xml configuration to
Guice Module to Dao to Service to Page ...

It's quite straightforward and the @Inject directives should be obvious but
maybe there is something else you might notice that will help.

-Luther



*web.xml*

    <filter>
        <filter-name>wicket</filter-name>

<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
          <param-name>applicationFactoryClassName</param-name>

<param-value>org.apache.wicket.guice.GuiceWebApplicationFactory</param-value>
        </init-param>
        <init-param>
            <param-name>module</param-name>

<param-value>org.effectiveprogramming.effprog.web.MainGuiceModule</param-value>
        </init-param>
        <init-param>
            <param-name>configuration</param-name>
            <!-- development | deployment -->
            <param-value>development</param-value>
        </init-param>
    </filter>


*MainGuiceModule.java*

public class MainGuiceModule extends AbstractModule
{
    /**
     * @see com.google.inject.AbstractModule#configure()
     */
    @Override
    protected void configure()
    {
        bind(WebApplication.class).to(MainApplication.class);

        // archive
        bind(ArchiveDao.class).to(ArchiveDaoImpl.class);
        bind(ArchiveService.class).to(ArchiveServiceImpl.class);

        // authentication
        bind(UserDao.class).to(UserDaoImpl.class);
        bind(UserService.class).to(UserServiceImpl.class);
        bind(EncryptionService.class).to(EncryptionServiceImpl.class);

        // cleanup
        bind(TagCleanupStrategy.class).to(SimpleTagCleanupStrategy.class);

        // category
        bind(CategoryDao.class).to(CategoryDaoImpl.class);
        bind(CategoryService.class).to(CategoryServiceImpl.class);

        // post
        bind(PostDao.class).to(PostDaoImpl.class);
        bind(PostService.class).to(PostServiceImpl.class);

        // profile
        bind(ProfileDao.class).to(ProfileDaoImpl.class);
        bind(ProfileService.class).to(ProfileServiceImpl.class);

        // tag
        bind(TagDao.class).to(TagDaoImpl.class);
        bind(TagService.class).to(TagServiceImpl.class);
    }
}


*PostDaoImpl.java*

@SuppressWarnings("unchecked")
public class PostDaoImpl implements PostDao
{
    @Inject
    private DaoHelper<Post> $;

    /**
     * @see
org.effectiveprogramming.effprog.service.persistence.dao.PostDao#delete(org.effectiveprogramming.effprog.entity.Post)
     */
    public void delete(final Post post)
    {
        $.delete(post);
    }


*PostServiceImpl.java*

public class PostServiceImpl implements PostService
{
    @Inject
    private ArchiveDao archiveDao;

    @Inject
    private CategoryDao categoryDao;

    @Inject
    private PostDao postDao;

    @Inject
    private TagDao tagDao;


*PostEditorPage.java*

public class PostEditorPage extends BasicLayout implements
IHeaderContributor
{
    public PostEditorPage()
    {
        super(new ResourceModel("head-title"));

        // panel
        final Panel feebackPanel = new FeedbackPanel("feedback-panel");
        feebackPanel.setOutputMarkupPlaceholderTag(true);
        feebackPanel.setOutputMarkupId(true);
        add(feebackPanel);

        // post
        final Post post = new Post();
        final PostForm form = new PostForm("new-post", post, feebackPanel);
        add(form);
    }

    public PostEditorPage(final Post post)
    {
        super(new ResourceModel("head-title"));

        // panel
        final Panel feebackPanel = new FeedbackPanel("feedback-panel");
        feebackPanel.setOutputMarkupPlaceholderTag(true);
        feebackPanel.setOutputMarkupId(true);
        add(feebackPanel);

        // post
        final PostForm form = new PostForm("new-post", post, feebackPanel);
        add(form);
    }

    private static class PostForm extends StatelessForm<Post>
    {
        private static final long serialVersionUID = 1L;

        private static final List<String> EMPTY_LIST =
Collections.emptyList();

        @Inject
        private CategoryService categoryService;

        @Inject
        private PostService postService;

        @Inject
        private TagService tagService;

        @Inject
        private TagCleanupStrategy tagCleanupStrategy;


*UnpublishedPostsPage.java*

public class UnpublishedPostsPage extends BasicLayout implements
IHeaderContributor
{
    @Inject
    private PostService postService;

    public UnpublishedPostsPage()
    {
        super(new ResourceModel("head-title"));

        // Who am I anyway?
        final UserSession sess = (UserSession) Session.get();
        final User user = sess.getUser();

        // display posts in a panel
        final Panel panel = new PostsPanel("posts-panel", new
UnpublishedPostsModel(postService, user), PublishedFlag.UNPUBLISHED);
        add(panel);
    }


On Fri, Jun 26, 2009 at 12:39 PM, Igor Vaynberg <[email protected]>wrote:

> it is wicket-guice module that wraps the service that is retrieved
> from guice with a serializable proxy, not guice itself.
>
> this module treats pages and components exactly the same way because
> page IS A component. i am not sure why pages work differently for you
> if you use the same @Inject annotation and letting the module do the
> injection for you rather then guice itself.
>
> if you take the service that is causing a problem on a page and inject
> that into a component it works fine? can you verify that the thing
> being injected into a page is the wicket serializable proxy rather
> then the direct instance?
>
> -igor
>
> On Fri, Jun 26, 2009 at 10:20 AM, Aaron Dixon<[email protected]> wrote:
> > But the nice thing about Guice/Wicket is that you don't have to
> > concern yourself with the serialization problem. And for my non-page
> > components, injection works perfectly. (This is because Guice injects
> > a *serializable* proxy not an actual instance of the service itself.
> > On detach/serialization, the proxy is serialized and on
> > deserializaiton/rehydration, Guice/Wicket will inject the
> > proxy/service again.)
> >
> > So, to reiterate, my problem is that all of this works perfectly for
> > me for any non-page components, but when I @Inject on a Page, the
> > service that is injected is not a proxy and therefore not
> > serializable... and therefore I get the serialization exceptions.
> >
> > Technically Guice IS injecting my service at the page level, it's just
> > injecting the instance itself and not wrapping it a proxy, whereas it
> > IS injecting serializable proxies for non-page components. Why?
> >
> >
> > On Thu, Jun 25, 2009 at 2:04 PM, Mauro Ciancio<[email protected]>
> wrote:
> >> On Wed, Jun 24, 2009 at 1:38 PM, Aaron Dixon <[email protected]> wrote:
> >>
> >>>
> >>>
> org.apache.wicket.util.io.SerializableChecker$WicketNotSerializableException:
> >>> Unable to serialize class:
> >>> com.mycompany.dao.MyDao$$EnhancerByGuice$$3e6e9f94
> >>> Field hierarchy is:
> >>>  2 [class=com.mycompany.pages.MyPage, path=2]
> >>>    private com.mycompany.dao.MyDao
> >>>
> >>>
> com.mycompany.pages.MyPage.myDao[class=com.mycompany.dao.MyDao$$EnhancerByGuice$$3e6e9f94]
> >>> <----- field that is not serializable
> >>>    at
> >>>
> >>>
> org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:342)
> >>>    at
> >>>
> >>>
> org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:610)
> >>> ...
> >>> ====
> >>>
> >>> Has anyone else noticed this?
> >>>
> >>
> >>  Yes, it happens because the injected class isn't Serializable and when
> >> your page
> >> is serialized the exception is thrown.
> >>  You could use for example a
> >> LoadableDetachableModel<
> http://wicket.apache.org/docs/wicket-1.3.2/wicket/apidocs/org/apache/wicket/model/LoadableDetachableModel.html
> >to
> >> detach your DAO when
> >> your page is serialized.
> >>  Guice also comes with an interface named Provider<T>. This could helps
> >> because it
> >> dont hold a reference to your un-serializable object.
> >>
> >> Example:
> >>
> >> class MyPage extends Page {
> >>  LoadableDetachableModel daoModel = new loadable() {
> >>    object get() {
> >>          return new dao();
> >>     }
> >>  };
> >>
> >>  void something() {
> >>     mydao = daoModel.getObject();
> >>     //stuff
> >>  }
> >>
> >>  void ondetach() {
> >>    super.ondetach()
> >>     daomodel.detach();
> >>   }
> >>  }
> >>
> >> HTH
> >> Cheers!
> >> --
> >> Mauro Ciancio
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>

Reply via email to