@Context对象来自何处 [英] Where do @Context objects come from
问题描述
什么是服务器(glassfish为我的问题)的机制是什么?我的问题到处寻找,但似乎无法找到明确的答案... )注入用@Context注释的实际objets?更具体地说,如果我想编写一个类似的类:
@Path(/)
public class MyResource {
@GET
public String doSomething(@Context MyObject obj){
// ...
}
}
那我该怎么做呢?
编辑:我见过类似以下内容的内容:
在@Context,@Provider和ContextResolver中使用JAX-RS
http://jersey.576304.n2.nabble.com/ContextResolver-confusion-td5654154.html
然而,这并没有与我见过的东西不一样,例如在org.neo4j.server.rest.web.RestfulGraphDatabase的构造函数中,它具有以下签名:
public RestfulGraphDatabase(
@Context UriInfo uriInfo,
@Context数据库数据库,
@Context InputFormat输入,
@Context OutputFormat输出,
@Context LeaseManager leaseManager)
您可以编写自己的注入提供程序并将其插入泽西岛 - 查看 SingletonTypeInjectableProvider 和PerRequestTypeInjectableProvider - 扩展其中一个类(取决于您希望的生命周期注射对象),并在您的Web应用程序中将您的实现注册为提供者。
For exa就像这样:
@Provider
public class MyObjectProvider extends SingletonTypeInjectableProvider< Context,MyObject> {
public MyObjectProvider(){
//将MyObject.class绑定到单个MyObject实例
//即,如果您使用
//将创建MyObject创建的实例@Context MyObject myObject
super(MyObject.class,new MyObject());
要在你的web应用中包含提供者,你有几个选项:如果您的应用使用类路径扫描(或包扫描),只需确保提供程序位于正确的软件包/类路径中
- / li>
- ,或者您可以使用META-INF / services条目(添加META-INF / services / com.sun.jersey.spi.inject.InjectableProvider文件来注册它,其名称为您的提供者类在内容中)
I've been searching everywhere, but can't seem to find a clear answer...
What is the mechanism whereby a server (glassfish for my problem) injects actual objets that are annotated with @Context? More specifically, if I wanted to write a class that did something like:
@Path("/")
public class MyResource {
@GET
public String doSomething(@Context MyObject obj) {
// ...
}
}
then how would I do it? Where is it that the MyObject is instanciated, who does it, and how?
Edit: I've seen stuff like the following:
Using @Context, @Provider and ContextResolver in JAX-RS
http://jersey.576304.n2.nabble.com/ContextResolver-confusion-td5654154.html
However, this doesn't square with what I've seen, e.g. in the constructor of org.neo4j.server.rest.web.RestfulGraphDatabase, which has the following signature:
public RestfulGraphDatabase(
@Context UriInfo uriInfo,
@Context Database database,
@Context InputFormat input,
@Context OutputFormat output,
@Context LeaseManager leaseManager )
解决方案 You can write your own injection provider and plug that into Jersey - look at SingletonTypeInjectableProvider and PerRequestTypeInjectableProvider - extend one of these classes (depending on the lifecycle you want for the injectable object) and register your implementation as a provider in your web app.
For example, something like this:
@Provider
public class MyObjectProvider extends SingletonTypeInjectableProvider<Context, MyObject> {
public MyObjectProvider() {
// binds MyObject.class to a single MyObject instance
// i.e. the instance of MyObject created bellow will be injected if you use
// @Context MyObject myObject
super(MyObject.class, new MyObject());
}
}
To include the provider in your web app you have several options:
- if your app uses classpath scanning (or package scanning) just make sure the provider is in the right package / on the classpath
- or you can simply register it using META-INF/services entry (add META-INF/services/com.sun.jersey.spi.inject.InjectableProvider file having the name of your provider class in it's contents)
这篇关于@Context对象来自何处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!