Search in sources :

Example 1 with SpringIntoHK2Bridge

use of org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge in project jersey by jersey.

the class SpringComponentProvider method initialize.

@Override
public void initialize(InjectionManager injectionManager) {
    this.injectionManager = injectionManager;
    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine(LocalizationMessages.CTX_LOOKUP_STARTED());
    }
    ServletContext sc = injectionManager.getInstance(ServletContext.class);
    if (sc != null) {
        // servlet container
        ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
    } else {
        // non-servlet container
        ctx = createSpringContext();
    }
    if (ctx == null) {
        LOGGER.severe(LocalizationMessages.CTX_LOOKUP_FAILED());
        return;
    }
    LOGGER.config(LocalizationMessages.CTX_LOOKUP_SUCESSFUL());
    // initialize HK2 spring-bridge
    HK2InjectionManager hk2InjectionManager = (HK2InjectionManager) injectionManager;
    SpringBridge.getSpringBridge().initializeSpringBridge(hk2InjectionManager.getServiceLocator());
    SpringIntoHK2Bridge springBridge = injectionManager.getInstance(SpringIntoHK2Bridge.class);
    springBridge.bridgeSpringBeanFactory(ctx);
    // register Spring @Autowired annotation handler with HK2 ServiceLocator
    Binder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bind(new AutowiredInjectResolver(ctx)).to(InjectionResolver.class);
            bind(ctx).to(ApplicationContext.class).named("SpringContext");
        }
    };
    injectionManager.register(binder);
    LOGGER.config(LocalizationMessages.SPRING_COMPONENT_PROVIDER_INITIALIZED());
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Binder(org.glassfish.jersey.internal.inject.Binder) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SpringIntoHK2Bridge(org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ServletContext(javax.servlet.ServletContext) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager)

Aggregations

ServletContext (javax.servlet.ServletContext)1 HK2InjectionManager (org.glassfish.jersey.hk2.HK2InjectionManager)1 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)1 Binder (org.glassfish.jersey.internal.inject.Binder)1 SpringIntoHK2Bridge (org.jvnet.hk2.spring.bridge.api.SpringIntoHK2Bridge)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1