Spring的設定檔

來源:互聯網
上載者:User

標籤:word   資料庫   class   基於   包含   tor   mon   let   取數   

Web.xml將會配置Spring的設定檔位置:

    <servlet>
        <servlet-name>x</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:x-servlet.xml</param-value>
        </init-param>
    </servlet>

這樣就會到classes下面去找x-servlet.xml來進行配置。

x-servlet.xml裡麵包含了幾個部分:

1. 靜態資源,預設基於springmvc的工程是不允許訪問靜態資源,比如js,.jpge等,需要指定靜態資源檔案夾,這樣,該檔案夾下面的資源可以被訪問:

  <mvc:resources mapping="/resources/**" location="/resources/" />

2. 註解類進行自動載入,拋棄了之前的配置(applicationContext.xml),基於註解的模式把開發人員的體力釋放出來了。在這些檔案夾下面定義的@Controller, @Resource等註解將會被自動載入到Spring容器中進行管理。

  <context:component-scan base-package="lorrywork.emall.domain"/>

3. 定義Bean,bean分為幾類:

3.1 定義了JSP-Bean頁面位置,從這個開始,就是定義bean了,bean分為幾類

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

3.2 定義Jdbc-Bean,這裡注意定義了context:property-placeholder,這個好處是只需要定義資料庫資訊在一個地方。比如對於servlet-init階段,無法通過spring-mybatis,可能需要直接使用JDBC,這樣只需要在properties中定義一次,既可以直接讓JDBC訪問,也可以讓後續的mybatise從該設定檔中擷取資料庫資訊,這樣只需要定義一個地方即可。

    <context:property-placeholder location="classpath:config.properties"/>
    <bean id="jdbcDataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${dburl}" />
        <property name="username" value="${uid}" />
        <property name="password" value="${pwd}" />
    </bean>

3.3 mybatis

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="jdbcDataSource" />
        <!-- 自動掃描mapping.xml檔案 -->
        <property name="mapperLocations" value="classpath*:lorrywork/emall/dao/map/*/*.xml" />
    </bean>
    <!-- DAO類(Mapper類)介面所在包名,Spring會自動尋找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="lorrywork.emall.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

 

Spring的設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.