【问题标题】:jersey-spring3: Injecting Jersey OAuth resource into Spring managed Beanjersey-spring3:将 Jersey OAuth 资源注入 Spring 托管 Bean
【发布时间】:2014-06-27 12:48:00
【问题描述】:

我正在尝试使用 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" />


但是,当我启动我的 web 应用程序时,我不断收到此异常:

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)。对此的任何帮助将不胜感激!

【问题讨论】:

  • 这里的问题是“ServiceLocatorName”参数需要设置为Jersey使用的定位器的名称。不幸的是,我认为 Jersey 使用未命名的 ServiceLocators,所以这可能是一个问题。 SpringScopeImpl 类采用另一个设置器,即 ServiceLocator 本身,但您必须以编程方式将其连接到 Spring 中,我不知道该怎么做(但我敢打赌这是可能的)。让我对此进行一些研究......
  • 感谢您回复@jwells131313。我非常感谢你关注这个!如果有我可以使用的解决方法,请告诉我。
  • @jwells131313 .. 有关解决方法的任何更新?

标签: spring oauth dependency-injection jersey hk2


【解决方案1】:

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

以下是在 Spring 中自动装配“OAuth1Signature”实例所需要做的事情,因为我们需要 hk2 到 spring 桥接以在 Spring 中注入 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.完成以上2个步骤后,就可以自动绑定“OAuth1Signature”了。

@Autowired
private OAuth1Signature oAuth1Signature;

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2011-12-20
    • 2011-11-05
    相关资源
    最近更新 更多