Springプロファイル[servlet-name]-servlet.xml

11223 ワード

注記コントローラの概要:
spring 2.5以前はcontrollerインタフェースまたは実装によってプロセッサクラスを定義していた.
spring2.5注記@controllerと@requestmappingによるプロセッサクラスの定義のサポートを開始します.D e f a u l t AnnotationHandlerMaping、AnnotationMethodHandlerAdapterは@controllerと@requestmappingのサポートを提供します.
spring3.0 restfulアーキテクチャスタイルのサポートを導入し、より多くの注釈を導入しました.
spring3.1新しいHandlerMappingおよびHandlerAdapterを使用して、@controller、@requestmapping注記プロセッサクラス、requesthandlermappingadapter、およびrequesthandlermappingをサポートします.
Springの[servletname]-servlet.xmlプロファイルは、次の3つのステップに分けられます.
1.スキャンパッケージの構成
<context:component-scan base-package="com.tgb.web.controller.annotation"> </context:component-scan>

springが起動すると自動的にスキャンパッケージの下に@Component@が含まれますController@Serviceこれらの注釈のクラスを待ってbeanに登録します.従来の方法で直接コンテナにコントローラを登録することもできます.例を参照してください.
注意:が構成されている場合、ラベルはxmlで構成する必要はありません.前者には後者が含まれているためです.さらには、以下の2つのサブラベルを提供しており、具体的な機能はここでは説明しない.
     1).    
     2.)    
ファイルヘッダにcontextを次のように宣言する必要があります.
xmlns:context="http://www.springframework.org/schema/context"

を使用して、SpringコンテナにA u t w i r e d AnnotationBeanPostProcessor、R e q u i r e d AnnotationBeanPostProcessor、C o m o n n o t i o t i o n BeanPostProcessor、P e r s i s i s t e n c e n t AnnotationBeanPostProcessorの4つのBeanPostProcessorを暗黙的に登録することで、@Autowired、@Resource、@PostConstruct、@PreDestroyなどの注釈を系統的に認識できます.従来の登録方式は以下の通りである(比較的煩雑である):
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

 
2.プロセッサ・マッピングとプロセッサ・アダプタの構成
    <!--Spring3.1      HandlerMapping -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 

    

    <!--Spring3.1      HandlerAdapter -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

        <!--       session-->

        <property name="synchronizeOnSession" value="true"/>

    </bean> 

@controllerおよび@requestmappingのサポートを提供します(この注釈サポートをオンにします).
注意:を使用して、以上のプロセッサマッピングとプロセッサ適合beanの構成の代わりに使用することもできます.
は、このような簡略化された形式の代わりに手動で構成することができ、簡略化された形式は、初期学習でデフォルトの構成スキームを迅速に適用することができます.は、spring MVCが@Controllersに要求を配信するために必要な、D e f a u l t AnnotationHandlerMappingとAnnotationMethodHandlerAdapterの2つのbeanを自動的に登録します.データバインドサポート、@NumberFormatannotationサポート、@DateTimeFormatサポート、@Validサポート、読み書きXMLサポート(JAXB)、読み書きJSONサポート(Jackson)を提供しています.
3.ビュー解析器の構成
    <!-- ViewResolver -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

        <property name="prefix" value="/WEB-INF/jsp/"/>

        <property name="suffix" value=".jsp"/>

    </bean>

前/接尾辞解析ビューを追加します.
 
 
プロファイルは次のとおりです.
<?xml version="1.0" encoding="UTF-8"?>

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

    </context:component-scan>



    <!--Spring3.1      HandlerMapping -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 

    

    <!--Spring3.1      HandlerAdapter -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

        <!--       session-->

        <property name="synchronizeOnSession" value="true"/>

    </bean> 

       

    <!-- ViewResolver -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

        <property name="prefix" value="/WEB-INF/jsp/"/>

        <property name="suffix" value=".jsp"/>

    </bean>

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

  
</beans>