Skip to content

Instantly share code, notes, and snippets.

@matthauck
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthauck/ae2565d0f2afde56b3a1 to your computer and use it in GitHub Desktop.
Save matthauck/ae2565d0f2afde56b3a1 to your computer and use it in GitHub Desktop.
jersey2 spring bridge
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");
// ...
}
}
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) {
}
}
}
<!-- ... -->
<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>
<!-- ... -->
@matthauck
Copy link
Author

@goecho
Copy link

goecho commented Jul 16, 2014

how it used?

@matthauck
Copy link
Author

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