Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将协作者连接到Jersey资源?_Java_Jersey - Fatal编程技术网

Java 如何将协作者连接到Jersey资源?

Java 如何将协作者连接到Jersey资源?,java,jersey,Java,Jersey,我有一种启动应用程序的方法: 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 = GrizzlyWe

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

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);
}
处理依赖关系的正确方法是什么?我希望我的资源
与其他对象通信,如何将它们连接在一起?

如果您喜欢使用spring,那么最好的方法是使用模块(作为附加依赖项安装)。它与球衣同步发布

有一个很好的例子可以让您开始。

您可以实现。例如:

@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();
            }
        };
    }
}
@Provider
公共类FoodProvider
实现InjectableProvider{
公共组件范围getScope(){
返回ComponentScope.PerRequest;
}
公共可注入getInjectable(组件上下文、资源、类型){
返回新的可注射(){
公共对象getValue(){
返回新的Foo();
}
};
}
}
然后在资源中注释字段:

@资源私有富富


谢谢,肖恩。如果我根本不使用XML,你有什么办法让它工作吗?我像这样启动我的应用程序:GrizzlyWebContainerFactory.create(baseUri,initParams);或者有没有一种方法可以让我不用spring?我现在介绍spring有点过头了。
@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();
            }
        };
    }
}