1. 程式人生 > >ssh框架優化配置檔案位置

ssh框架優化配置檔案位置

優化我們專案中的配置檔案資訊及位置可以幫助我們構建更清晰的專案結構,也更方便尋找指定的配置檔案。
先來看看各配置檔案的位置:
spring主配置檔案:applicationContext.xml,預設在WEB-INF目錄下
struts2主配置檔案:struts.xml,預設在類的根路徑下
hibernate主配置檔案:hiberntae.cfg.xml,預設在類的根路徑下
web專案配置檔案:web.xml,預設在WEB-INF下

目的:
1.將hibernata.cfg.xml的內容放到applicationContext.xml中,刪去hibernate.cfg.xml
2.將applicationContext.xml移動到專門的包下
3.將struts2移動到專門的包下
4.將applicationContext.xml分為若干個模組,在applicationContext.xml中引用這些模組。例如專門配置dao層的applicationContext-dao.xml

步驟實施:

一.將hibernata.cfg.xml的內容放到applicationContext.xml中,刪去hibernate.cfg.xml

applicationContext.xml中與hibernate.cfg.xml相關的:

<!-- 配置SessionFactory -->
<bean id="sessionFactory"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	<!--這裡引入了hibernate.cfg.xml,刪去 -->
	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

原來的sessionFactory中的property標籤指定了hibernate.cfg.xml的位置,現在將這個位置刪除,然後改為三部分的資訊:連線資料庫資訊,hibernate可選配置,對映檔案位置

<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 連線資料庫的資訊 用連線池-->
		<property name="dataSource" ref="dataSource"></property>
		<!-- hibernate的可選配置 -->
		<property name="hibernateProperties">
			<props>
				<!-- 資料庫的方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 是否顯示hibernate生成的SQL語句 -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 是否使用格式化輸出sql語句到控制檯 -->
				<prop key="hibernate.format_sql">false</prop>
				<!-- 配置hibernate採用何種方式生成DDL語句 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- 把session和執行緒繫結,從而實現一個執行緒只有一個Session<prop key="hibernate.current_session_context_class">thread</prop> -->
			</props>
		</property>
		<!-- 對映檔案的位置 -->
		<property name="mappingLocations”>
			<array>
				<value>classpath:com/cm/domain/*.hbm.xml</value>
			</array>
		</property>
	</bean>

引入的資料庫連線池的資訊:

<!-- 配置連線池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/xxx"></property>
		<property name="user" value="mysql的使用者名稱"></property>
		<property name="password" value="密碼"></property>
	</bean>

此時可刪除hibernate.cfg.xml了

二.將applicationContext.xml移動到專門的包下
從web-inf中轉移
在web.xml中手動指定spring的配置檔案位置,需要使用servletContext的初始化引數
web.xml中新增:

<!-- 手動指定spring的配置檔案位置,需要使用servletContext的初始化引數 -->
  <!-- param-name有指定值 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:config/spring/applicationContext.xml</param-value>
  </context-param>

即可將applicationContext.xml放到config.spring包下了

三.將struts2移動到專門的包下
struts.xml是由struts2的核心過濾器載入的,在web.xml中:

<!-- struts2的核心過濾器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
      <param-name>struts.devMode</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

裡面已經有一個init-param了,再新增一組:

<init-param>
      <param-name>config</param-name>
      <param-value>struts-default.xml,struts-plugin.xml,config/struts/struts.xml</param-value>
</init-param>

注意,只能更改第二個逗號後的值,此時就可以將struts.xml移動到config.struts包下

四.分配置檔案編寫spring和struts2的配置檔案
1.例:將applicationContext.xml中的dao部分內容寫出去(與資料庫相關的),然後在applicationContext.xml中引入分出去的配置檔案
引入其他spring配置檔案:applicationContext.xml中:

	<!-- 引入其他spring配置檔案 -->
	<import resource="applicationContext-dao.xml" />

與dao層有關的配置有:
hibernateTemplat,sessionFactory,連線池dataSource

此時,可刪除applicationContext.xml中關於dao的配置
2.struts.xml中,可以類似的引入其他struts2配置檔案

	<include file="config/struts/struts-xxx.xml" ></include>

注意,這個要寫完整的路徑,包名…/檔名

ok,大功告成。