Last active
August 29, 2015 14:03
-
-
Save matthauck/ae2565d0f2afde56b3a1 to your computer and use it in GitHub Desktop.
jersey2 spring bridge
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
package com.foo; | |
import javax.inject.Inject; | |
import org.glassfish.hk2.api.ServiceLocator; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.springframework.context.ApplicationContext; | |
public class JerseyConfig extends ResourceConfig { | |
@Inject | |
public JerseyConfig(ServiceLocator locator) { | |
// Integrate spring w/ jersey | |
ApplicationContext ctx = JerseySpringBridge.getApplicationContext(locator); | |
JerseySpringBridge.integrate(ctx, locator); | |
// register(SomeFeature.class); | |
// ... | |
// packages("com.foo.something"); | |
// ... | |
} | |
} | |
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
package com.foo; | |
import javax.servlet.ServletContext; | |
import javax.ws.rs.Path; | |
import java.util.HashSet; | |
import java.util.Set; | |
import org.glassfish.hk2.api.DynamicConfiguration; | |
import org.glassfish.hk2.api.Factory; | |
import org.glassfish.hk2.api.ServiceLocator; | |
import org.glassfish.hk2.utilities.binding.ServiceBindingBuilder; | |
import org.glassfish.jersey.internal.inject.Injections; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.web.context.support.WebApplicationContextUtils; | |
/** | |
* implement the spring integration ourselves since jersey's spring3 module | |
* requires integration with hk2's injection system and is quite slow | |
* | |
* @author mhauck | |
*/ | |
public class JerseySpringBridge { | |
public static ApplicationContext getApplicationContext(ServiceLocator locator) { | |
return WebApplicationContextUtils.getWebApplicationContext( | |
locator.getService(ServletContext.class) | |
); | |
} | |
/** | |
* Integrate spring w/ jersey/hk2 | |
*/ | |
public static void integrate(ApplicationContext ctx, ServiceLocator locator) { | |
DynamicConfiguration config = Injections.getConfiguration(locator); | |
for (ResourceBean bean : resourceBeans(ctx)) { | |
ServiceBindingBuilder<Object> binding = Injections.newFactoryBinder( | |
new SpringLookup(ctx, bean) | |
); | |
binding.to(bean.type); | |
Injections.addBinding(binding, config); | |
} | |
config.commit(); | |
} | |
private static Set<ResourceBean> resourceBeans(ApplicationContext ctx) { | |
Set<ResourceBean> resources = new HashSet<>(); | |
for (String beanName : ctx.getBeanDefinitionNames()) { | |
Class<?> beanType = ctx.getType(beanName); | |
if (isResource(beanType)) { | |
resources.add(new ResourceBean(beanName, beanType)); | |
} | |
} | |
return resources; | |
} | |
protected static boolean isResource(Class<?> type) { | |
return type.isAnnotationPresent(Path.class); | |
} | |
private static class ResourceBean { | |
final String name; | |
final Class<?> type; | |
public ResourceBean(String name, Class<?> type) { | |
this.name = name; | |
this.type = type; | |
} | |
} | |
private static class SpringLookup implements Factory<Object> { | |
private ResourceBean resource; | |
private ApplicationContext ctx; | |
SpringLookup(ApplicationContext ctx, ResourceBean resource) { | |
this.ctx = ctx; | |
this.resource = resource; | |
} | |
@Override | |
public Object provide() { | |
return ctx.getBean(resource.name, resource.type); | |
} | |
@Override | |
public void dispose(Object instance) { | |
} | |
} | |
} |
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
<!-- ... --> | |
<servlet> | |
<servlet-name>jersey</servlet-name> | |
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> | |
<init-param> | |
<param-name>javax.ws.rs.Application</param-name> | |
<param-value>com.foo.JerseyConfig</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<!-- ... --> |
how it used?
sorry for the lag. updated the gist w/ how to use it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For context, see https://java.net/jira/browse/JERSEY-2578