一个标准的Spring配置文件的写法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--读取数据库连接配置文件-->
<bean
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
    <!--数据库连接配置文件路径及文件名-->
    <value>/WEB-INF/jdbc.properties</value>
    <!--数据库连接配置文件如果放在SRC目录下则用以下配置-->
    <!--
    <value>classpath:jdbc.properties</value>
    -->
   </property>
</bean>

<bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource"
   destroy-method="close">
   <property name="driverClassName"
    value="${jesun.driverClassName}" />
   <property name="url" value="${jesun.url}" />
   <property name="username" value="${jesun.username}" />
   <property name="password" value="${jesun.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <!-- 配置数据源 -->
   <property name="dataSource">
    <ref local="dataSource"/>
   </property>
   <!-- 配置hibernate映射文件 -->
   <property name="mappingLocations">
    <list>
     <value>classpath:/com/jesun/**/*.hbm.xml</value>
    </list>
   </property>
   <!-- 以上配置映射文件还可以这样写(写具体的映射文件路径,但有点麻烦) -->
   <!-->property name="mappingResources">
    <list>
     <value>com/jesun/test/test.hbm.xml</value>
    </list>
   </property-->
   <property name="hibernateProperties">
    <props>
     <!--
     <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> -->
     <!--prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop -->
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.jdbc.batch_size">50</prop>
     <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
   </property>
</bean>

<!-- 定义hibernate的sessionFactory
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation"
    value="classpath:hibernate.cfg.xml">
   </property>
</bean> -->
<!-- 定义事务管理器 -->
<bean id="transactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref local="sessionFactory" />
   </property>
</bean>
<!-- 暂时不用
   <bean id="baseTranProxy" abstract="true"
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
     <property name="transactionAttributes">
    <props>
  
     <prop key="save*">PROPAGATION_REQUIRED</prop>
     <prop key="update*">PROPAGATION_REQUIRED</prop>
      <prop key="delete*">PROPAGATION_REQUIRED</prop>
     <prop key="find*">PROPAGATION_REQUIRED</prop>
   
     <prop key="*">readOnly</prop>
    </props>
    </property>
   </bean>
-->

<bean id="transactionInterceptor"
   class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager">
    <ref bean="transactionManager" />
   </property>
   <!-- 配置事务传播特性 -->
   <property name="transactionAttributes">
    <props>
     <prop key="save*">PROPAGATION_REQUIRED</prop>
     <prop key="update*">PROPAGATION_REQUIRED</prop>
     <prop key="delete*">PROPAGATION_REQUIRED</prop>
     <prop key="find*">PROPAGATION_REQUIRED</prop>
     <prop key="*">readOnly</prop>
    </props>
   </property>
</bean>

<bean id="autoproxy"
   class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <!-- 为所有以Svr结尾的Bean添加事务(业务层) -->
   <property name="beanNames">
    <list>
     <value>*Svr</value>
    </list>
   </property>
   <property name="interceptorNames">
    <list>
     <value>transactionInterceptor</value>
    </list>
   </property>
</bean>
</beans>

其中,jdbc.properties文件的内容如下:
#----------------------MySql-------------------------
jesun.driverClassName=com.mysql.jdbc.Driver
#jesun.url=jdbc:mysql://localhost:3306/websites
jesun.url=jdbc:mysql://localhost:3306/websites?useUnicode=true&characterEncoding=UTF-8
jesun.username=root
jesun.password=root
#---------------------MsSql--------------------------
#jesun.driverClassName=net.sourceforge.jtds.jdbc.Driver
#jesun.url=jdbc:jtds:sqlserver://127.0.0.1:1433/websites
#jesun.username=sa
#jesun.password=sa
#---------------------Oracle-------------------------
#jesun.driverClassName=oracle.jdbc.driver.OracleDriver
#jesun.url=jdbc:oracle:thin:@192.168.0.219:1521:orcl
#jesun.username=root
#jesun.password=root

项目web.xml文件的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
   <description>配置struts2过滤器</description>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
   <description>加载spring</description>
   <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
<context-param>
   <description>加载所有spring配置文件</description>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/*-spring.xml</param-value>
</context-param>
  
<filter>
   <description>使用openSessionInVew模式,保证事务一致性</description>
   <filter-name>OpenSessionInViewFilter</filter-name>
   <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>OpenSessionInViewFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100441.html