spring的配置文件怎么写?配置struts2的和配置SessionFactory的!!!那位大侠给来个例子!!!

解决方案 »

  1.   

    <?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-lazy-init="true"> <description>Spring公共配置文件 </description> <!-- 定义受环境影响易变的变量 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
    <list>
    <!-- 标准配置 -->
    <value>classpath*:/application.properties</value>
    <!-- 本地开发环境配置 -->
    <value>classpath*:/application.local.properties</value>
    <!-- 服务器生产环境配置 -->
    <!-- <value>file:/var/myapp/application.server.properties</value> -->
    </list>
    </property>
    </bean> <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="org.springside.examples.miniweb" /> <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <!-- Connection Info -->
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info -->
    <property name="initialSize" value="5" />
    <property name="maxActive" value="100" />
    <property name="maxIdle" value="30" />
    <property name="maxWait" value="1000" />
    <property name="poolPreparedStatements" value="true" />
    <property name="defaultAutoCommit" value="false" />
    </bean> <!-- 数据源配置,使用应用服务器的数据库连接池 -->
    <!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB" />--> <!-- Hibernate配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="namingStrategy">
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
    </prop>
    <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
    </props>
    </property>
    <property name="packagesToScan" value="org.springside.examples.miniweb.entity.*" />
    </bean> <!-- 事务管理器配置,单数据源事务 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean> <!-- 事务管理器配置,多数据源JTA事务-->
    <!--
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager or
    WebLogicJtaTransactionManager" />
    --> <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
      

  2.   

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
            "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    <constant name="struts.convention.default.parent.package" value="crud-default" />
    <constant name="struts.convention.package.locators" value="web" /> <!-- 用于CRUD Action的parent package -->
    <package name="crud-default" extends="convention-default">
    <!-- 基于paramsPrepareParamsStack,
    增加store interceptor保证actionMessage在redirect后不会丢失 -->
    <interceptors>
    <interceptor-stack name="crudStack">
    <interceptor-ref name="store">
    <param name="operationMode">AUTOMATIC</param>
    </interceptor-ref>
    <interceptor-ref name="paramsPrepareParamsStack" />
    </interceptor-stack>
    </interceptors> <default-interceptor-ref name="crudStack" />
    </package> <!-- 
    使用Convention插件,实现约定大于配置的零配置文件风格.
               特殊的Result路径在Action类中使用@Result设定. 
    -->
    </struts>
      

  3.   

    applicationContext-common.xml    在公共里配置SessionFactory
    <!-- 配置 sessionfactory,, -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="configLocation">
              <value>classpath:hibernate.cfg.xml</value>
           </property>
        </bean>
      

  4.   

    摘自springside,LZ可以参考一下springside的sample
      

  5.   

    完整的配置文件  后面是事务的处理 applicationContext-common.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-2.5.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-2.5.xsd"
        >
        
        <!-- 配置 sessionfactory,, -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="configLocation">
              <value>classpath:hibernate.cfg.xml</value>
           </property>
        </bean>
     
        
        <!--配置事务管理器  -->
         <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
           <property name="sessionFactory">
              <ref bean="sessionFactory"/>
           </property>
         </bean>
         
         
         
        <!-- 配置事务特性 --> 
         <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
               <tx:method name="add*" propagation="REQUIRED"/>
               <tx:method name="del*" propagation="REQUIRED"/>
               <tx:method name="update*" propagation="REQUIRED"/>
               <tx:method name="*" read-only="true"/>           
            </tx:attributes>
         </tx:advice>
         
         
        <!-- 配置切入点 --> 
         <aop:config>
            <aop:pointcut id="allMethod" expression="execution (* com.org.oa.service.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
         </aop:config>
         
         
         
        </beans>