首页 > 使用 Jax-RS,如何使 @Path 修饰的根资源类为单例的?

使用 Jax-RS,如何使 @Path 修饰的根资源类为单例的?

@Path("helloworld")
public class HelloWorldResource {
    
    public HelloWorldResource() {
        System.out.println("New HelloWorldResource...");
    }
    
    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @Produces("text/plain")
    public String sayHello() {
         // Return the textual content
        return "Hello World";
    }
    
}

每次请求时,HelloWorldResource 都会 new 一个实例,如何将资源类设置为单例的?


使用 @Singleton(com.sun.jersey.spi.resource.Singleton) 修饰资源类,可以使资源类为单例的。

【热门文章】
【热门文章】