New features in Spring 2.5:spring MVC

Source: Internet
Author: User
Tags new features

The spring framework is dedicated to providing powerful, non-intrusive solutions to complex issues from the very beginning of creation. In spring 2.0, a custom namespace feature was introduced to reduce the number of XML configuration files, from which it was deeply rooted in the core Spring framework (AOP, context, Jee, JMS, Lang, TX, and Util namespaces), Spring Portfolio projects (such as spring security) and non-spring projects (for example, CXF).

Spring 2.5 introduces a complete set of annotations as an alternative to xml-based configuration. Annotations can be used for automatic discovery of spring Management objects, dependency injection, lifecycle methods, Web-tier configuration, and unit/integration testing.

Explore the annotation technology introduced in Spring 2.5 the article is composed of three parts, the second of which is mainly about annotation support in the Web layer. The final article will focus on other features that you can use for integration and testing.

The first part of this series discusses how Java annotations (annotation) is used to configure spring management objects and dependency injection instead of XML. Let's take another example to review:

@Controller
public class ClinicController {
   private final Clinic clinic;
   @Autowired
   public ClinicController(Clinic clinic) {
    this.clinic = clinic;
   }
   ...

@Controller indicates that Cliniccontroller is a web-tier component, @Autowired request a clinic instance that is dependent on injection. This example requires a small number of XML statements to enable the container to identify two annotations and to limit the scanning scope of the component:

<context:component-scan base-package="org.springframework.samples.petclinic"/>

This is a boon to the Web layer, as the XML configuration files on this layer of spring are becoming bloated and may not even be as useful as the configuration under the layers. The controller has many attributes, such as the view name, form object name, and validator type, which are more about configuration and less about dependency injection. You can also effectively manage similar configurations by defining inheritance through the bean, or by avoiding properties where configuration changes are not very frequent. In my experience, however, many developers will not do this, and the result is that XML files are always bigger than they really are. However @Controller and @autowired can have a positive effect on the configuration of the Web tier.

In the second part of the series, we will continue to discuss this issue and explore the annotation techniques in the web layer of Spring 2.5. These annotations are informally referred to as @mvc, which involves spring MVC and Spring porlet MVC, and in fact most of the features discussed in this article can be applied to both frameworks.

From Controller to @controller

Compared to the annotations discussed in the first section, @MVC is not only as simple as a replacement scheme for configuration, but consider the following famous Spring MVC controller signature:

public interface Controller {
   ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse
response) throws Exception;
}

All spring MVC controllers either implement the Controller interface directly or expand similar abstractcontroller, Simpleformcontroller, A base class implementation such as Multiactioncontroller or Abstractwizardformcontroller. It is the Controller interface that allows the dispatcherservlet of spring MVC to think of all of the above objects as "processors (handlers)" and in a named Simplecontrollerhandleradapter the help of the adapter to use them downwards.

@MVC changed the program design model from three important aspects:

No interfaces or base classes are required.

Any number of request processing methods are allowed.

Has a high degree of flexibility in the method signature.

Given the above three points, it is fair to say that @mvc is not just a replacement, it will be the next important step in the evolution of the controller technology of spring MVC.

Dispatcherservlet in the adapter named Annotationmethodhandleradapter Help to downgrade the controller with the annotation. It is this adapter that does a lot of work to support the annotations that we will discuss later, and it effectively replaces the requirements for the Controller base class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.