Skip to content

Instantly share code, notes, and snippets.

@artgon
Created November 7, 2011 09:52
Show Gist options
  • Save artgon/1344580 to your computer and use it in GitHub Desktop.
Save artgon/1344580 to your computer and use it in GitHub Desktop.
Extension of RestletServlet to allow Spring integration
public class SpringRestletServlet extends ServerServlet
{
private static final long serialVersionUID = 3178425324679869158L;
private static final Logger LOG = LoggerFactory.getLogger( SpringRestletServlet.class );
/**
* Override the createApplication method to inject Spring objects
*
* @param context - Restlet context
* @return - application instance
*/
@Override
public Application createApplication( Context context )
{
JaxRsApplication application = (JaxRsApplication) super.createApplication( context );
// set up spring to be the object creator
application.setObjectFactory(
new SpringObjectFactory( getWebApplicationContext().getAutowireCapableBeanFactory() )
);
return application;
}
private static class SpringObjectFactory implements ObjectFactory
{
private final AutowireCapableBeanFactory beanFactory;
public SpringObjectFactory( AutowireCapableBeanFactory beanFactory )
{
this.beanFactory = beanFactory;
}
@Override
public <T> T getInstance( Class<T> jaxRsClass ) throws InstantiateException
{
LOG.info( "Wiring JAX-RS class [" + jaxRsClass + "] using Spring." );
return beanFactory.createBean( jaxRsClass );
}
}
private WebApplicationContext getWebApplicationContext()
{
return WebApplicationContextUtils.getRequiredWebApplicationContext( getServletContext() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment