第九篇:Spring的applicationContext.xml配置總結
阿新 • • 發佈:2018-11-09
在前面的一篇日誌中,記錄了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.xsdhttp://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>資料庫的配置資訊,一般寫成配置檔案jdbc.properties,放在專案中,在專案啟動時載入讀取
</list> </property> </bean>
<!-- 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配置檔案要掌握的最基礎的配置使用資訊