How to wire in a collaborator into a Jersey resource?

前端 未结 2 364
滥情空心
滥情空心 2020-12-16 06:16

I have a method to start up my application:

public void start() throws IOException {
    final Map initParams = new HashMap

        
相关标签:
2条回答
  • 2020-12-16 07:09

    If you're comfortable using spring, then the best way is to use the jersey spring api module (installed as an additional dependency). It's released in lockstep with Jersey.

    The javadoc page has a decent example to get you started.

    0 讨论(0)
  • 2020-12-16 07:12

    You can implement InjectableProvider. For example:

    @Provider
    public class FooProvider
        implements InjectableProvider<Resource, Type> {
    
        public ComponentScope getScope() {
            return ComponentScope.PerRequest;
        }
    
        public Injectable getInjectable(ComponentContext ic, Resource resource, Type type) {
            return new Injectable() {
                public Object getValue() {
                    return new Foo();
                }
            };
        }
    }
    

    and then annotate field in your resource:

    @Resource private Foo foo;

    0 讨论(0)
提交回复
热议问题