Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features worth paying attention to in Spring3.2

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what the new features of Spring3.2 are worth paying attention to. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Once web.xml is no longer needed

In the new Spring 3.2, the file web.xml is no longer needed, thanks to the new features of Servlet 3.0.

Where you can use @ WebServlet to set the mapping relationship in servlet using annotations in the servlet that needs to be called. In this way, it is no longer convenient to configure servlet in web.xml as it used to be.

In addition, Servlet 3. 0 provides a way to dynamically register servlet in the container, as well as by implementing the

The method implementation of the ServletContainerInitializer interface dynamically registers Servlet, Filter, and listeners for the container during the container startup phase. During the startup phase of the application, the container calls the onStartup () method in all the classes that implement the ServletContainerInitializer interface. In Spring 3.2, this is further simplified by simply implementing the WebApplicationInitializer interface, which provides a related implementation class-AbstractContextLoaderInitializer, which can register DispatcherServlet dynamically. This means that as long as the spring-webmvc.jar is placed in the web-inf/lib of the web application, Dispatcher servlet can be called. You can refer to the following example (from the Spring documentation):

Public class MyWebApplicationInitializer implements WebApplicationInitializer {@ Override public void onStartup (ServletContext container) {ServletRegistration.Dynamic registration = container.addServlet ("dispatcher", new DispatcherServlet ()); registration.setLoadOnStartup (1); registration.addMapping ("/ example/*");}}

Second, support Java programmer configuration in Spring mvc 3.2. a good feature is that it further simplifies the configuration on the basis of supporting the use of Java code to configure various dependency injection in previous versions. The benefits of using Java programmatic configuration can be learned through the introduction to the following link article

(http://blog.frankel.ch/consider-replacing-spring-xml-configuration-with-javaconfig).

In Spring mvc 3.2, a subclass AbstractAnnotationConfigDispatcherServletInitializer of AbstractContextLoaderInitialize is provided to achieve the effect of zero AbstractContextLoaderInitialize configuration. You only need to create a class that inherits AbstractAnnotationConfigDispatcherServletInitializer, as shown in the following code:

Public class SugarSpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {@ Override protected Class [] getRootConfigClasses () {return new Class [] {JavaConfig.class};} @ Override protected Class [] getServletConfigClasses () {return new Class [] {WebConfig.class};} @ Override protected String [] getServletMappings () {return new String [] {"/"};}}

Three more powerful Spring Test frameworks now, unit testing is becoming more and more important, and each class is recommended to do a corresponding unit test. Prior to Spring 3.2, if you were to unit test Spring MVC, you had to explicitly call a method in a controller class, rather than unit testing the associated mapping mapping directly. In Spring mvc 3.2, the testing framework is reintegrated and enhanced to support direct mapping such as / * to test classes in a controller. At the same time, the previous open source project (https://github.com/SpringSource/spring-test-mvc) has also been included in Spring mvc 3.2. In addition, new test improvements such as return, redirect, and model have been added. Here is an example:

Public class SayHelloControllerIT extends AbstractTestNGSpringContextTests {private MockMvc mockMvc; @ BeforeMethod public void setUp () {mockMvc = webAppContextSetup ((WebApplicationContext) applicationContext). Build ();} @ Test (dataProvider = "pathParameterAndExpectedModelValue") public void accessingSayhelloWithSubpathShouldForwardToSayHelloJspWithModelFilled (String path, String value) throws Exception {mockMvc.perform (get ("/ sayHello/Jo")) .andexpect (view (). Name ("sayHello")) .andexpect (model (). Attribute ("name", "Jo")) }} this is the end of the article on "what are the new features worth paying attention to in Spring3.2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report