jersey-spring3:将Jersey OAuth资源注入Spring托管Bean

时间:2014-05-09 17:10:24

标签: spring oauth dependency-injection jersey hk2

我正在尝试使用Spring托管bean插入Jersey 2.7资源。具体来说,我想在Spring bean中注入OAuth1Signature,如下所示:

@Component
public class OAuthManager {

    @Inject
    private OAuth1Signature oAuthSignature;

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


我尝试使用HK2 Spring集成文档中提供的说明:HK2 Spring Integration。在文档之后,我将其添加到我的spring xml配置中:

<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" />


但是,当我启动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文档声明ServiceLocator应该由Jersey 2.7使用的HK2框架注入。我对如何让Spring使用jersey-spring3桥为我实例化OAuth1Signature感到困惑,因为它似乎不知道服务定位器的来源。

我尝试过搜索StackOverflow和其他Jersey消息板,但大多数都处理相反的用例(在Jersey资源中注入spring bean)。任何有关这方面的帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我最近在我的项目中完成了OAuth的开发,在那里我使用Jersey 2.9.1和Spring。

以下是在Spring中自动装配“OAuth1Signature”实例所需要做的事情,因为我们需要hk2来桥接以在春天注入hk2服务。

1.定义自定义hk2范围

@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.在“hk2”范围内定义OAuth1Signature bean

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

3.完成上述两个步骤后,您已准备好自动装配“OAuth1Signature”。

@Autowired
private OAuth1Signature oAuth1Signature;

干杯