1. 程式人生 > >Maven搭建SSH案例(四)-----配置檔案的優化

Maven搭建SSH案例(四)-----配置檔案的優化

使用Maven搭建好SSH框架之後,為了以後更方便的修改,以及對之前的配置檔案進行簡化,現在將使用幾種方法來簡化配置:

1、利用jdbc.properties檔案來通過變數在applicationContext.xml對資料庫資訊進行配置

jdbc.properties配置 :

#資料庫基本配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/csuft_news?autoReconnect=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=zengyongsheng

jdbc.maxOpenPreparedStatements=20
jdbc.initialSize=10
jdbc.maxActive=10
jdbc.maxIdle=10
jdbc.minIdle=10
jdbc.maxWait=10000

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=update
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.default_batch_fetch_size=30
hibernate.format_sql=true

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">
        
        <!-- 讀取src/main/resource下jdbc屬性檔案 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	        <property name="locations">
	            <list>
	               <value>classpath*:/jdbc.properties</value>
	            </list>
	        </property>
        </bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="poolPreparedStatements" value="false" /> <property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}" /> <property name="maxActive" value="${jdbc.maxActive}" /> <property name="maxIdle" value="${jdbc.maxIdle}" /> <property name="maxWait" value="${jdbc.maxWait}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <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> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop> <prop key="hibernate.connection.release_mode">after_transaction</prop> </props> </property>
<property name="mappingLocations"> <list> <value>classpath:/com/zys/system/**/model/*.hbm.xml</value> </list> </property> </bean> </beans>

2、在web.xml中配置引數載入spring的配置檔案applicationContext.xml,然後將applicationContext.xml放到src/main/resource下(預設applicationContext.xml是放置WEB_INF下),這樣可以便於管理,還可以建立一個spring_config.xml專門用來注入物件。

web.xml新增配置:

 <!-- 讀取根目錄下的所有.xml配置檔案 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/*.xml,</param-value>
  </context-param>

spring_config.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">
                
		<bean id="sysUserService" class="com.zys.system.service.SysUserService"></bean>
        <bean id="sysUserDao" class="com.zys.system.dao.SysUserDao"></bean>
        <bean id="loginService" class="com.zys.system.service.LoginService"></bean>                
</beans>

3.修改Maven的本地倉庫地址