如何将协作者连接到 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 = 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");
}
}
正确的方法是什么处理依赖关系?我希望我的资源 与其他对象通信,如何将它们连接在一起?
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);
}
And I have a Jersey resource class:
@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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以实现 InjectableProvider。例如:
然后在资源中注释字段:
You can implement InjectableProvider. For example:
and then annotate field in your resource:
如果您习惯使用 spring,那么最好的方法是使用 jersey spring api 模块(作为附加依赖项安装)。它与 Jersey 同步发布。
javadoc 页面 有一个不错的示例可以帮助您入门。
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.