1 Mart 2018 Perşembe

SpringMVC WebApplicationInitializer Arayüzü

Giriş
Şu satırı dahil ederiz
import org.springframework.web.WebApplicationInitializer;
WebApplicationInitializer Ne İşe Yarar ?
Açıklaması şöyle. Yani web.xml ile yerine kodla Servletcontext nesnesini yaratılmasını sağlar
With the WebApplicationInitializer, Spring Framework Web MVC applications can now rid themselves of web.xml. Additional configuration/implementations are available for programmatically registering the DispatcherServlet and configuring the Spring container
WebApplicationInitializer Arayüzünü Kim Çalıştırır
Örnek
Şöyle yaparız.
public class Initializer implements WebApplicationInitializer {

  public void onStartup(ServletContext servletContext)
    throws ServletException {
    AnnotationConfigWebApplicationContext ctx = 
      new AnnotationConfigWebApplicationContext();

    ctx.scan("com.package.name");
    //the rest of initialization
  }
}
Örnek
Şöyle yaparız.
public class WebAppInitializer implements WebApplicationInitializer{
   @Override
  public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context =
      new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.abc.webapp.config");
    container.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcherServlet",
      new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
}

Hiç yorum yok:

Yorum Gönder