Question

I am trying to insert a Jersey 2.7 resource withing a Spring managed bean. Specifically, I want to inject OAuth1Signature within a Spring bean like so:

@Component
public class OAuthManager {

    @Inject
    private OAuth1Signature oAuthSignature;

    private void someMethod() {
        String signature = oAuthSignature.generate(oauthRequest, params, secrets);
    }
}


I have tried using instructions provided within the HK2 Spring integration document: HK2 Spring Integration. Following the document, I added this to my spring xml configuration:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="hk2">
                <bean class="org.jvnet.hk2.spring.bridge.api.SpringScopeImpl" >
                  <property name="ServiceLocatorName" value="HK2ToSpringTest" />
                </bean>
            </entry>
        </map>
    </property>
</bean>

<bean id="org.glassfish.jersey.oauth1.signature.OAuth1Signature"
      class="org.glassfish.jersey.oauth1.signature.OAuth1Signature"
      scope="hk2" 
      lazy-init="true" />


However, I keep getting this exception when I start my webapp:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching       bean of type [org.glassfish.hk2.api.ServiceLocator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:952)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)


OAuth1Signature documentation states that the ServiceLocator is supposed to be injected by HK2 framework which Jersey 2.7 uses. I am very confused on how I can get Spring to instantiate OAuth1Signature for me using the jersey-spring3 bridge since it does not seem to know where the Service locator should come from.

I have tried searching through StackOverflow and other Jersey message boards, but most of them deal with the opposite use case (injecting spring beans in a Jersey resource). Any help on this would be greatly appreciated !

Was it helpful?

Solution

I have recently done the development for OAuth in my project where I used Jersey 2.9.1 with Spring.

Below is what needs to be done to autowire the "OAuth1Signature's" instance in the Spring as we require hk2 to spring bridge to inject the hk2 services in the spring.

1.Define the custom hk2 scope

@Bean
public static CustomScopeConfigurer scopeConfigurer() {

    Map<String, Object> scopeMap = new HashMap<String, Object>();
    SpringScopeImpl hk2SpringScope = new SpringScopeImpl();
    CustomScopeConfigurer customScopeConfigurer = new CustomScopeConfigurer();

    hk2SpringScope.setServiceLocatorName("hk2SpringLocator");
    scopeMap.put("hk2", hk2SpringScope);
    customScopeConfigurer.setScopes(scopeMap);

    return customScopeConfigurer;
}

2.Define the OAuth1Signature bean in "hk2" scope

@Bean(name = "oauth1Signature")
@Scope("hk2")
public OAuth1Signature getOAuth1Signature() {
    ServiceLocator hk2ServiceLocator = ServiceLocatorFactory.getInstance()
            .find("hk2SpringLocator");
    OAuth1Signature oAuth1Signature = new OAuth1Signature(hk2ServiceLocator);
    return oAuth1Signature;
}

3.After you are done with above 2 steps, you are ready to the autowire the "OAuth1Signature".

@Autowired
private OAuth1Signature oAuth1Signature;

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top