Created
November 7, 2011 09:52
-
-
Save artgon/1344580 to your computer and use it in GitHub Desktop.
Extension of RestletServlet to allow Spring integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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