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