第九篇:Spring的applicationContext.xml配置總結


在前面的一篇日志中,記錄了web.xml配置啟動的順序,web啟動到監聽器ContextLoaderListener時,開始加載spring的配置文件applicationContext.xml(通常就叫這個名字),在查詢大量資料之后決心將該文件詳細的配置說明和講解記錄下來,以供查閱,加深原理的理解。
        首先是spring的各類命名空間和xml聲明:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
</beans>
        這一部分, 可以自行查閱資料,不去深究也罷。
        以下配置,均在<beans></beans>標簽之類,為了便於講解,直接寫出來,一般的在這個配置文件中主要做兩件事,掃描注解和注冊數據源(數據庫信息相關)

第一,掃描注解,看下面兩個標簽
1、<context:component-scan base-package="cn.byan" use-default-filters="false"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
2、<context:annotation-config />
第1種標簽:
base-package填入你要掃描的包
use-default-filters這個 屬性表示你要掃描注解的類,
true,默認會掃描注解了這四個標志的類:Service,Component,Repository,Controller;
false,如果你設為false,那么就需要增加 <context:exclude-filter/>這個子節點,指出哪種注解不讓spring去默認掃描
當設為false時,需要掌握子節點的這幾個屬性:
<context:exclude-filter/>:該子節點指定要排除掃描的類,比如一般的@controller的注解類可能會放到mvc的配置文件中去指定掃描
<context:include-filter/>:相反,該子節點指定要掃描的類,什么時候用,還沒遇到過
type :通常都是annotation(注解的意思)
expression :注解類的全名(每種注解類在spring中都有全名)
第2種標簽:
<context:annotation-config />,也是spring用的比較多的默認掃描的配置,功能和1差不多,但是第一種配置包含第2種配置,所以直選其一即可。1用的比較多

以上標簽,通俗點講,就是:web啟動時,默認掃描cn.byan包中注解了@service,@Component,@Repository標注的類,交給spring容器去管理(不掃描@Controller標注的類)。

第二,配置數據源
<!-- 用spring提供的PropertyPlaceholderConfigurer讀取數據庫配置信息-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
     <list>
    <value>classpath:conf/jdbc.properties</value>
  </list>   </property> </bean>
數據庫的配置信息,一般寫成配置文件jdbc.properties,放在項目中,在項目啟動時加載讀取

<!-- dataSource讀取數據庫的配置信息 ,這里用的是dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" init-method="init" destroy-method="close">
  <property name="driverClassName" value="${jdbc_driverClassName}"></property>
  <property name="url" value="${jdbc_url}"></property>
  <property name="username" value="${jdbc_username}"></property>
  <property name="password" value="${jdbc_password}"></property>
  <!-- 連接池最大使用連接數 -->
  <property name="maxActive" value="${jdbc_maxActive}"></property>
  <!-- 初始化連接大小 -->
  <property name="initialSize" value="${jdbc_initialSize}"></property>
  <!-- 獲取連接最大等待時間 -->
  <property name="maxWait" value="${jdbc_maxWait}"></property>
  <!-- 連接池最大空閑 -->
  <property name="maxIdle" value="${jdbc_maxIdle}"></property>
  <!-- 連接池最小空閑 -->
  <property name="minIdle" value="${jdbc_minIdle}"></property>
  <!-- 自動清除無用連接 -->
  <property name="removeAbandoned" value="${jdbc_removeAbandoned}"></property>
  <!-- 清除無用連接的等待時間 -->
  <property name="removeAbandonedTimeout" value="${jdbc_removeAbandonedTimeout}"></property>
  <!-- 事物是否自動提交-->
  <property name="defaultAutoCommit" value="${jdbc_defaultAutoCommit}"></property>
  <!-- 連接屬性 -->
  <property name="connectionProperties" value="clientEncoding=UTF-8"></property>
</bean> 

<!-- 配置SessionFactory,這里用的是hibernate4,mybatis目前還不太熟 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.byan.entity" />
  <property name="hibernateProperties">
  <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  </props>
  </property>
</bean> 

<!-- 定義事務管理器,用了spring管理的hibernate4的事物,再也不用擔心事物。。。-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean> 

<!-- 聲明事務管理器,此時在ServiceImpl或者DaoImpl類上需加上@Transactional注解-->
<tx:annotation-driven transaction-manager="transactionManager" />
以上是applicationContext.xml配置文件要掌握的最基礎的配置使用信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM