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的本地仓库地址


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖墩有点瘦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值