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,大功告成。

猜你喜欢

转载自blog.csdn.net/weixin_40616523/article/details/86139029
今日推荐