在尝试使用OSGi中的单例资源嵌入RESTEasy时(使用与resteasy-osgi-bundle类似的东西),令我惊讶的是,现场注入@Context
UriInfo
可用并且在每个请求上都有效.
进一步挖掘我发现代理魔术和ThreadLocal
在ResteasyProviderFactory
.一切都很好,但我无法在docs中找到任何对这种行为的引用,既不是在RESTEasy中也不是在JAX-RS规范中.
在Jersey文档中,我们可以找到类似的内容:
特定请求对象存在例外,它甚至可以注入构造函数或类字段[ 具有单例范围的资源 - OP].对于这些对象,运行时将注入能够同时服务更多请求的代理.这些请求的对象是
HttpHeaders
,Request
,UriInfo
,SecurityContext
.可以使用@Context
注释注入这些代理.
它在RESTEasy中看起来如何?目前的实施是稳定还是实验?可以注入单例的特定于请求的类的集合是什么?
我正在使用require.js,而我的一个模块又需要另一个模块,但是对它的导出(这是一个jQuery插件)不感兴趣,即对代码不感兴趣
define(['jquery', 'jquery.mousewheel', 'fabric'], function ($, something, fabric) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我对没兴趣something
。当然,我可以将不感兴趣的结果的依存关系移到数组的末尾,而只对函数省略相应的“ trailing”参数,但是出于可读性考虑,我希望将它们保持如图所示。这就引出了我的问题...
在函数定义中标记被忽略的参数的JavaScript惯用法是什么?
当然,我知道我可以使用任何阻止使用变量的名称:
someMethodWithCallback(1, 2, 3, function (dx, notinterested1, notinterested2, point) {
// ...
});
Run Code Online (Sandbox Code Playgroud)
对于以下各项(至少在Chrome中)有效(并且因为undefined不是关键字),我感到很惊讶:
someMethodWithCallback(1, 2, 3, function (dx, undefined, undefined, point) {
// ...
});
Run Code Online (Sandbox Code Playgroud)
但是当然改变undefined
函数内部的值可能很容易出错...
要忽略Perl具有的参数值undef
,StandardML具有_
; JavaScript是否至少在“民俗”级别上具有类似的功能?
我正在编写自定义JAX-RS 2.0应用程序(在Jersey 2.3.1下),其中包含一些供所有资源使用的数据。
public class WebApp extends org.glassfish.jersey.server.ResourceConfig {
public WebApp() {
packages("my.resources.package");
}
}
Run Code Online (Sandbox Code Playgroud)
(我也可以使用API javax.ws.rs.core.Application
,描述的结果是相同的)
然后我将对象注入资源
@Path("test")
public class Test {
@Context
Application app;
@GET
@Path("test")
public String test() {
return "Application class: " + app.getClass();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,通话结果是
Application class: class org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig
Run Code Online (Sandbox Code Playgroud)
这让我用了一些丑陋的把戏
Application class: class org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig
Run Code Online (Sandbox Code Playgroud)
我对JAX-RS 2.0规范第9.2.1节的理解:
Application
可以使用@Context
注释将应用程序提供的子类的实例注入到类字段或方法参数中。对子Application
类实例的访问允许将配置信息集中在该类中。注意,不能将其注入Application
子类本身,因为这将创建循环依赖。
是应用程序提供的Application
子类是mine WebApp
,而不是JAX-RS实现特定的包装器。
此外,更改此片段
if (app instanceof WebApp) {
return (WebApp) app;
} else if …
Run Code Online (Sandbox Code Playgroud) 在JAX-RS Web应用程序中,我们使用子资源:
@Path("/some/things")
public class ThingsListResource {
@Inject
SomeStorage store;
@GET
public List<Thing> getAllThings() {
return store.getAllThings();
}
@Path("{id}")
public ThingResource getThingResource(@PathParam("id") String id) {
return new ThingResource(id); // PROBLEMATIC
}
}
public class ThingResource {
@Inject
SomeOtherDependecy dep;
@Inject
SomeStorage store;
private final String id;
public ThingResource(String id) {
this.id = id;
}
@GET
public Thing getThisThing() {
return store.getThing(id);
}
@DELETE
public void removeThisThing() {
store.removeThing(id);
}
// following is a list of methods useful enough
// to …
Run Code Online (Sandbox Code Playgroud) 在编译a时Client
,它使用一些接口I
(例如O
)的实现,类文件I
也必须存在于类路径中.奇怪的是,这只是一个例子javac
,因为Eclipse编译器(ECJ)不需要I
编译.
是什么让JDK 需要超类型进行编译,ECJ编译得很好?
它不是默认方法,如错误报告中所评论的那样,兼容性指南也同意:
在针对另一个实现在另一个类文件中定义的接口的类编译类时,这样的类文件(其中定义了接口)必须在编译期间由javac使用的类路径中可用.这是JDK 8的新要求 - 如果不这样做将导致编译错误.
更新:
I.doit()
是default
简单的抽象方法都没关系,行为是一样的I.doit()
被覆盖,当然都很重要O
; 如果没有覆盖,那么ECJ也会达到I
定义doit()
接口(api/a/I.java
):
package a;
public interface I {
default void doit() {
System.out.println("In I");
}
}
Run Code Online (Sandbox Code Playgroud)
实施(impl/b/O.java
):
package b;
public class O implements a.I {
public void doit() {
System.out.println("In O"); …
Run Code Online (Sandbox Code Playgroud)