임베디드 서블릿 엔진이 서블릿을 적극적으로 인스턴스화하도록 만드는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/75379

  •  09-06-2019
  •  | 
  •  

문제

문제는 간단하지만 이미 조금 어려움을 겪고 있습니다.

Server server = new Server(8080);  
Context context = new Context(server, "/", Context.NO_SESSIONS);
context.addServlet(MainPageView.class, "/");
context.addServlet(UserView.class, "/signup");
server.start();

이는 Jetty 세계 어디에서나 찾을 수 있는 매우 표준적인 코드 조각입니다.Jetty를 서블릿 엔진으로 포함하고 일부 서블릿을 포함하는 애플리케이션이 있습니다.

이러한 서블릿 중 일부를 인스턴스화하려면 시작 시 많은 작업이 필요합니다.말하기 – 추가 구성 파일 읽기, 데이터베이스에 연결 등첫 번째 사용자 요청이 아닌 모든 어려운 작업을 미리 수행할 수 있도록 서블릿 엔진이 모든 서블릿을 즉시 인스턴스화하도록 하려면 어떻게 해야 합니까?

도움이 되었습니까?

해결책

Guice make의 Justin's 옵션을 사용하는 것이 왜 효과가 없는지 잘 모르겠습니다.정확히 무엇이 주입되나요?이것이 Justin이 위에 쓴 것과 매우 유사하기 때문에 이것이 도움이 될지 확실하지 않지만 이 방법으로 수행하면 Jetty가 실제로 인스턴스화를 수행합니다.

Context context = new Context(server, "/", Context.NO_SESSIONS);
ServletHolder mainPageViewHolder = new ServletHolder(MainPageView.class);
// Do this to force Jetty to instantiate the servlet
mainPageViewHolder.getServlet();  
context.addServlet(mainPageViewHolder, "/");

다른 팁

사용 Context.addServlet 걸리는 과부하 ServletHolder. ServletHolder 클래스 또는 서블릿 인스턴스를 허용하는 클래스입니다.

Servlet myServlet = new MyServlet();
ServletHolder holder = new ServletHolder(myServlet);
context.addServlet(holder, "/");

이는 Jetty 6을 가정합니다.Jetty 7에서도 작동할 것 같아요.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top