Struts-Spring-Hibernate(XML文件配置)

转载:http://blog.csdn.net/andymu077/archive/2008/06/06/2517191.aspx?P_AVPASS=PHDGBITAVPASSP

web.xml


载入Log4j配置
<context-param><!--Log4j配置 在同一容器中部署多个应用不能使用默认的webAppRootKey,必须指定唯一KEY,以免冲突-->
    <param-name>webAppRootKey</param-name>
    <param-value>itservice.root</param-value>
    <!--在log4j.properties中设置日志路径log4j.appender.file.File=${itservice.root}/WEB-INF/itservice.log-->
</context-param>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

载入Spring配置文件
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    <!--可载入多个配置文件分隔符-->
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

字符编码过滤器
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

配置延迟加载时使用OpenSessionInView
<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
      <param-name>singleSession</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>sessionFactoryBeanName</param-name>
       <!--指定对Spring配置中哪个sessionFactory使用OpenSessionInView-->
      <param-value>sessionFactory_itdb</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>




Struts-config.xml


<action input="/index.jsp"
        name="loginActionForm"
        parameter="method"
        path="/loginAction"
        scope="session"
        type="org.springframework.web.struts.DelegatingActionProxy"
        validate="true" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
     <!--将spring配置中关于ACTION的配置独立到一个action-servlet.xml文件中-->
      <set-property property="contextConfigLocation" value="/WEB-INF/classes/action-servlet.xml" />
</plug-in>


ApplicationContext.xml


<!--直接使用hibernate配置文件-->
<bean id="sessionFactory_itdb" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
         <value>classpath:hibernate_itdb.cfg.xml</value>
    </property>
</bean>

<!--使用JNDI DataSource-->
<bean id="it_dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>jdbc/itdb</value>
    </property>
</bean>

<!-- Spring配置DataSource -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
       <value>classpath:init.properties</value>
    </property>
  </bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${dataSource.driverClassName}"></property>
    <property name="url" value="${dataSource.url}"></property>
    <property name="username" value="${dataSource.username}"></property>
    <property name="password" value="${dataSource.password}"></property>
</bean>

<!-- *********************Hibernate*********************** -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="mappingResources">
        <list>
            <value>com/usish/shweb/hbm/ShwebFile.hbm.xml</value>
            <value>com/usish/shweb/hbm/ShwebLog.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.jdbc.batch_size">30</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
        </props>
    </property>
</bean>

<!--******************TransactionManager***********************-->
<bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
</bean>
<bean id="baseTxProxy"
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
      lazy-init="true"
      abstract="true">
    <property name="transactionManager">
        <ref bean="transactionManager" />
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="get*">PROPAGATION_REQUIRED</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
        </props>
  </property>
</bean>

你可能感兴趣的:(spring,xml,Hibernate,log4j,struts)