users@jersey.java.net

[Jersey] _at_PostConstruct not executed on Providers?

From: Martynas Jusevicius <martynas_at_graphity.org>
Date: Sat, 28 Jan 2012 13:37:32 +0100

Hey list,

I have a MessageBodyWriter class, which uses @PostConstruct to
initialize XSLT stylesheet located in the local WEB-INF folder and
accessed via ServletContext:

@Provider
@Singleton
@Produces({MediaType.TEXT_HTML})
public class ResourceXSLTWriter implements MessageBodyWriter<Resource>
{
    public static final String XSLT_BASE = "/WEB-INF/xsl/";

    private XSLTBuilder xslt = null;

    @PostConstruct
    public void init(@Context ServletContext context)
    {
            logger.log(Level.INFO, "@PostConstruct");
            xslt = XSLTBuilder.fromStylesheet(getStylesheet(context,
XSLT_BASE + "index.xsl")).
    }

    // ... more code
}

It is initialized as a Singleton in the Application:

    @Override
    public Set<Object> getSingletons()
    {
        singletons.add(new ResourceXSLTWriter());

        return singletons;
    }

However I can see that @PostConstruct is never called, therefore xslt
variable stays null and writeTo() throws an exception later on.

Anything I'm doing wrong?

Martynas
graphity.org