如何将合作者连接到泽西资源? [英] How to wire in a collaborator into a Jersey resource?

查看:13
本文介绍了如何将合作者连接到泽西资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法来启动我的应用程序:

I have a method to start up my application:

public void start() throws IOException {
    final Map<String, String> initParams = new HashMap<String, String>();
    initParams.put("com.sun.jersey.config.property.packages", "com.example");
    threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);
}

我有一个 Jersey 资源类:

@Path("/notification")
public class NotificationResource {
    // HOW DO I INJECT THIS GUY?
    private MySampleCollabolator  mySampleCollabolator;    

    @POST
    public void create() {
        System.out.println("Hello world"); 
    }
}

处理依赖关系的正确方法是什么?我希望我的资源与其他对象通信,如何将它们连接在一起?

What is the proper way of handling dependencies? I would like my resources to communicate with other objects, how do I wire them together?

推荐答案

可以实现InjectableProvider.例如:

@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;

@Resource private Foo foo;

这篇关于如何将合作者连接到泽西资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆