【问题标题】:How to inject a spring bean into Jersey InjectableProvider如何将 spring bean 注入 Jersey InjectableProvider
【发布时间】:2011-11-05 18:08:08
【问题描述】:

我刚刚为 Jersey 创建了一个 InjectableProvider 的实现,但我发现完全设置了我需要来自 Spring 工厂 bean 支持的注入。我正在寻找一种将工厂作为字段注入提供程序类的方法。我已经尝试过使用 @Component 注释,但是由于 IllegalState 异常(没有为范围“请求”注册的范围)失败)

处理这个问题的正确方法是什么?或者有没有办法从 Jerseys HTTPcontext 中获取应用程序上下文?

【问题讨论】:

    标签: spring inversion-of-control jersey provider


    【解决方案1】:

    这已经很晚了,但我讨厌看到没有答案的问题。

    通常,在您的工厂 bean 上,您应该有:

    @Component
    public class FactoryBean(){}
    

    在你的提供者中,你应该使用

    public class Provider extends InjectableProvider<Context>{
          @InjectParam
          private FactoryBean factoryBean;
    }
    

    乍一看,您会认为应该使用 @Autowired,因为它是 Spring 的“组件”——但由于我们将它连接到 Jersey 实例化 bean 中,因此应该使用 Jersey 的 @InjectParam。

    【讨论】:

    • 注意,InjectableProvider 实现中包含的“Context”是Jersey 注解@Context 中常用的URIInfo 和HttpRequest 对象。
    猜你喜欢
    • 2013-04-05
    • 2014-06-27
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多