How to use Spring’s WebApplicationInitializer

本文介绍了Spring MVC应用中DispatcherServlet的作用及配置方式的变化。从早期通过web.xml文件注册到利用Servlet 3.0规范及WebApplicationInitializer接口进行程序化配置的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



The DispatcherServlet has always served as the doorway component or “front controller” in a Spring Framework Web MVC application.  It’s job has been and still is to marshal requests to other components in the Spring MVC application backend and to orchestrate a response back to clients.

springmvc

DISPATCHERSERVLET AND THE WEB.XML

Until Spring 3.1, the DispatcherServlet had to be registered with the Servlet container via the standard Web application web.xml file. The following elements had to be added to register the DispatcherServlet.

With the presence of the Servlet 3.0 specification, the web.xml file in a Web application is now optional.  The Servlet 3.0 API brought annotations to the Java Web application world allowing the registration of servlets to be accomplished by annotation.

However, since you don’t write the DispatcherServlet, and the Spring community didn’t have the knowledge and details of your application to be able to annotate the DispatcherServlet for you, there was a bit of a quandary.  How do you get the DispatcherServlet registered with the Web container without web.xml?

THE WEBAPPLICATIONINITIALIZER

The answer was provided in Spring 3.1 (and of course is available with Spring 4 which came out in December 2013) with the WebApplicationInitializer.  An implementation of the WebApplicationInitializer interface configures the ServletContext programmatically.  In particular, it allows for the creation, configuration, and registration of the DispatcherServlet programmatically.  Thereby allowing the web.xml file to be removed from modern Spring MVC applications.  A simple implementation of WebApplicationInitializer is shown below.

When you need to customize the associated WebApplicationContext (i.e. the Spring container), you may prefer to build an instance of the Spring container in the WebApplicationInitializer and provide the container to the DispatcherServlet.

WRAP UP

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 – see here for more details.

Spring Framework 4

If you would like to learn more about the Spring Framework or Spring Framework Web MVC, I encourage you to take a look at Intertech’s line of Spring Framework training classes.  I just completed updating our Spring classes to Spring Framework 4.  There are a lot of new features in later versions of Spring 3 and now Spring 4.  Some of them have been covered in recent blog posts (see genericsmeta annotationsconditional bean configuration, and ordering autowired collections ), but you can learn more about them in our new classes.

The DispatcherServlet has always served as the doorway component or “front controller” in a Spring Framework Web MVC application.  It’s job has been and still is to marshal requests to other components in the Spring MVC application backend and to orchestrate a response back to clients.

springmvc

DISPATCHERSERVLET AND THE WEB.XML

Until Spring 3.1, the DispatcherServlet had to be registered with the Servlet container via the standard Web application web.xml file. The following elements had to be added to register the DispatcherServlet.

With the presence of the Servlet 3.0 specification, the web.xml file in a Web application is now optional.  The Servlet 3.0 API brought annotations to the Java Web application world allowing the registration of servlets to be accomplished by annotation.

However, since you don’t write the DispatcherServlet, and the Spring community didn’t have the knowledge and details of your application to be able to annotate the DispatcherServlet for you, there was a bit of a quandary.  How do you get the DispatcherServlet registered with the Web container without web.xml?

THE WEBAPPLICATIONINITIALIZER

The answer was provided in Spring 3.1 (and of course is available with Spring 4 which came out in December 2013) with the WebApplicationInitializer.  An implementation of the WebApplicationInitializer interface configures the ServletContext programmatically.  In particular, it allows for the creation, configuration, and registration of the DispatcherServlet programmatically.  Thereby allowing the web.xml file to be removed from modern Spring MVC applications.  A simple implementation of WebApplicationInitializer is shown below.

When you need to customize the associated WebApplicationContext (i.e. the Spring container), you may prefer to build an instance of the Spring container in the WebApplicationInitializer and provide the container to the DispatcherServlet.

WRAP UP

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 – see here for more details.

Spring Framework 4

If you would like to learn more about the Spring Framework or Spring Framework Web MVC, I encourage you to take a look at Intertech’s line of Spring Framework training classes.  I just completed updating our Spring classes to Spring Framework 4.  There are a lot of new features in later versions of Spring 3 and now Spring 4.  Some of them have been covered in recent blog posts (see genericsmeta annotationsconditional bean configuration, and ordering autowired collections ), but you can learn more about them in our new classes.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值