简体   繁体   English

每次请求都构造Jersey servlet类

[英]Jersey servlet class is constructed with every request

I am working on a very simple setup of tomcat and jersey servlet. 我正在对tomcat和jersey servlet进行非常简单的设置。 I just noticed that servlet class constructed for every request. 我只是注意到为每个请求构造的servlet类。 What I have read about servlets is that they are init() once, service() multiple times and destroy() once. 我对servlet的了解是它们一次是init(),多次是service(),一次是destroy()。 Why is it not the case for my setup. 为什么我的设置不是这种情况。

Below is my web.xml 下面是我的web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>contact-dropbox-servlet</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.inbhiwadi.services</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>contact-dropbox-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

And my servlet entry class looks like below: 我的servlet入口类如下所示:

@Path("/contact")
@Slf4j
public class ContactDropboxService {

    private ContactDAO contactDAO;

    private NotificationPublisher notificationPublisher;

    public ContactDropboxService() {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        this.contactDAO = (ContactDAO) context.getBean("contactDAO");
        this.notificationPublisher = (NotificationPublisher) context.getBean("notificationPublisher");
        log.debug("ContactDropboxService constructed one more time");
    }

    @GET
    public String greet() {
        log.info("Welcome to InBhiwadi contact services!");
        return "Welcome to InBhiwadi contact services";
    }

    @POST
    @Path("/drop")
    public Response create(Contact contact) {

        log.debug("Received contact is : [{}]", contact.toString());
        contactDAO.create(contact);
        notificationPublisher.publish(contact.toString());
        return Response.accepted("Contact dropped in box").build();
    }
}

What should I do to have single instance of ContactDropboxService serving multiple requests? 如何让ContactDropboxService的单个实例服务多个请求?

By default Jersey will instantiate resource class per request. 默认情况下,Jersey将为每个请求实例化资源类。 If you want to have just one instance of resource class you should annotate it with @Singleton. 如果只想使用一个资源类实例,则应使用@Singleton对其进行注释。 For more details you can check out this SO question 有关更多详细信息,您可以查看此SO 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM