我有一个方法来启动我的应用程序:
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);
}
Run Code Online (Sandbox Code Playgroud)
我有一个Jersey资源类:
@Path("/notification")
public class NotificationResource {
// HOW DO I INJECT THIS GUY?
private MySampleCollabolator mySampleCollabolator;
@POST
public void create() {
System.out.println("Hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
处理依赖项的正确方法是什么?我希望我的资源与其他对象进行通信,如何将它们连接在一起?
您可以实现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();
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
然后在资源中注释字段:
@Resource私人Foo foo;
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |