Spring configuration file [Servlet-name]-servlet.xml

Source: Internet
Author: User

Introduction to the Annotated controller:

The processor class is defined prior to spring2.5 by implementing the Controller interface or its implementation.

SPRING2.5 began to support the definition of processor classes through annotations @controller and @requestmapping, defaultannotationhandlermapping, Annotationmethodhandleradapter provides support for @controller and @requestmapping.

SPRING3.0 introduces restful architectural style support and introduces more annotations.

spring3.1 uses the new handlermapping and handleradapter to support @controller, @requestmapping annotation processor classes, Requesthandlermappingadapter and Requesthandlermapping.

The [servletname]-servlet.xml configuration file in spring is divided into three steps:

1. Configuring the Scan Package

<base-package= "com.tgb.web.controller.annotation"></  context:component-scan>

When spring starts, the classes that contain these annotations, such as @component @[email protected], are automatically scanned under the package and registered as beans. You can also register the controller directly in the container in the traditional way, see example.

Note: If <context:component-scan> is configured, the <context:annotation-config/> tag can be configured without XML, because the former contains the latter. In addition, <context:annotation-config/> also provides two sub-tags as follows, specific features are not described here.

1). <context:include-filter>

2.) <context:exclude-filter>

And you must declare the context in the file header as follows:

xmlns:context= "Http://www.springframework.org/schema/context"

Use <context:annotation-config/> implicitly to register autowiredannotationbeanpostprocessor with the spring container, Requiredannotationbeanpostprocessor, The 4 beanpostprocessor of Commonannotationbeanpostprocessor and persistenceannotationbeanpostprocessor enable the system to recognize @autowired ,@ Resource, @postconstruct,@ Predestroy, and other annotations . The traditional way to register is as follows (more cumbersome):

<class= " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

2. Configuring processor mappings and Processor adapters

    <!--Spring3.1 The beginning of the annotation handlermapping -    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>         <!--Spring3.1 The beginning of the annotation handleradapter -    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <!--thread-Safe Access session -        < Propertyname= "Synchronizeonsession"value= "true"/>    </Bean> 

Support for @controller and @requestmapping (enable the annotation support).

3. Configure the View resolver

    <!--Viewresolver -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView"/>        < Propertyname= "prefix"value= "/web-inf/jsp/"/>        < Propertyname= "suffix"value= ". jsp"/>    </Bean>

Plus the front/suffix resolution view.

A copy of the configuration file is as follows:

<? XML version= "1.0" encoding= "UTF-8" ?>

<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:p= "http://www.springframework.org/schema/p"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

    <Context:component-scanBase-package= "Com.tgb.web.controller.annotation">    </Context:component-scan>    <!--Spring3.1 The beginning of the annotation handlermapping -    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>         <!--Spring3.1 The beginning of the annotation handleradapter -    <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <!--thread-Safe Access session -        < Propertyname= "Synchronizeonsession"value= "true"/>    </Bean>            <!--Viewresolver -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView"/>        < Propertyname= "prefix"value= "/web-inf/jsp/"/>        < Propertyname= "suffix"value= ". jsp"/>    </Bean>

<!--referencing static resources--
<mvc:resources location= "/img/" mapping= "/img/**"/>

<!--The processor configuration, you can also register the controller here (while scanning the package is already registered in the container)--

<bean class= "Com.javass.mvc.web.controller.SuccessController"/>

<bean class= "Com.javass.mvc.web.controller.FinshController"/>

</ Beans >

Spring configuration file [Servlet-name]-servlet.xml

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.