Spring-Spring架構基本設定檔解析_spring

來源:互聯網
上載者:User

重點說明dispatcher-servlet.xml,applicationContext.xml,以及web.xml中與Spring有關的配置。 1. web.xml

dispatcher Servlet
Spring MVC內建了一個開箱即用的servlet,全名為org.springframework.web.servlet.DispatcherServlet,下面需要設定一個<load-on-startup>元素,存在併當值為1時,則在應用程式啟東時裝載這個servlet並調用init()方法初始化,而不是在第一個請求時載入,否則第一次請求時就無法起到請求轉寄的作用。

映射與定義dispatcherSerlvet

   <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>

其中還有一個<url-pattern>元素,意為轉寄哪一些URL結構的請求,當使用“/”時,表示所用請求都將先轉寄至dispatcherServlet,也有程式猿喜歡使用.do與.htm等尾碼。 context-param
context-param用於指定Spring IOC容器讀取BeanDefinition的XML檔案路徑,即存在<bean></bean>的檔案,也就是後面的applicationContext.xml。有一個內容為<param-name>contextConfigLocation</param-name>,此標識必須與另一個檔案的一個元素匹配,後面會說到。

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:/applicationContext.xml</param-value>    </context-param>
2.dispatcher-servlet.xml mvc:annotation-driven
Spring在配置此項後會自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter兩個Spring提供的Bean,以可以使用註解
<mvc:annotation-driven/>

context:annotation-config
隱式的向spring MVC註冊4個BeanPostProcess,之後可以解析一些注釋,例如@Autowired、@Resouse等

context:component-scan base-package=”/”
配置掃描路徑,Spring會自動掃描base-package下的檔案,如果存在使用了注釋的類或方法,則註冊它們為Bean,配置了此項,則不必配置context:annotation-config選項

<context:component-scan base-package="org.log.*"/>
ViewResolver
DispatcherSerlvet靠這個配置項解析在SpringMVC中Controller層返回給View層的頁面資訊,一般為String類型的路徑,可以在傳回值前後自動加上首碼與尾碼
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>            <property name="prefix" value="/WEB-INF/"/>            <property name="suffix" value=".jsp"/>        </bean>
3.applicationContext.xml mvc:resourse mapping=”/user/js/all” location=”/user/js”
配置用於自動載入靜態資源,如css、js、html
<mvc:resources mapping="/user/js/**" location="/user/js/"/>    <mvc:resources mapping="/user/css/**" location="/user/css/" />    <mvc:resources mapping="/user/images/**" location="/user/images/" />
bean id=”dataSource”
這個Bean元素用於更快捷、更簡便的去註冊資料庫驅動
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://localhost/users"/>        <property name="username" value="administrator"/>        <property name="password" value="administrator"/>    </bean>
mybatis
此處再列出兩個把Spring與Mybatis相容的配置Bean
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <property name="configLocation" value="classpath:/mybatis.xml"/>        <property name="mapperLocations" value="classpath:/mapper/*.xml"/>    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="org.log.mapper"/>        <property name="markerInterface" value="org.log.mapper.SqlMapper"/>    </bean>

本文章只能告訴你大概要使用Spring MVC所需的配置項,如果想要搞懂整個SpringMVC,這是遠遠不夠,只是當xml檔案中的配置報錯時,很難找出錯誤在哪裡,這時就可以參考此篇,看看是配置項少了,還是沒有配置對。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.