spring的配置文件记录

来源:互联网 发布:百度语音翻译软件 编辑:程序博客网 时间:2024/05/23 14:33

(1)datasource信息记录到properties文件中。properties文件加载到spring的容器中,通过${key}引用:

        <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config/jdbc.properties</value><value>classpath:config/datasource.properties</value></list></property><property name="ignoreResourceNotFound" value="true" /><property name="localOverride" value="true"></property></bean>


 

(2)配置扫描注解(主要配置扫描的包路径,注解类型)

         <context:component-scan base-package="com.boco"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>


 

(3)配置事务。(因为持久层采用的不同,spring的事务管理类也不同,如jdbc的事务管理类:DataSourceTransactionManager ,hibenate的事务管理:HibernateTransactionManager)

jdbc:

    <bean id="transactionManager"         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">         <property name="dataSource">             <ref bean="dataSource"/>         </property>     </bean>


hibernate:hibernate首先要配置sessionFactory,在配置HibernateTransactionManager

<!-- 配置HibernateSessionFactory工厂 -->     <bean id="sessionFactory"         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">         <property name="dataSource" ref="dataSource" />         <property name="mappingResources">             <list>                 <value>tarena/hbm/catelog.hbm.xml</value>                 <value>tarena/hbm/bookinfo.hbm.xml</value>                 <value>tarena/hbm/userinfo.hbm.xml</value>                 <value>tarena/hbm/order.hbm.xml</value>             </list>         </property>         <property name="hibernateProperties">             <props>                 <prop key="hibernate.dialect">                     org.hibernate.dialect.MySQL5Dialect                 </prop>                 <prop key="hibernate.query.substitutions">                     true 'Y', false 'N'                 </prop>                 <prop key="hibernate.show_sql">true</prop>             </props>         </property>     </bean>      <!-- 配置Hibernate事务管理器 -->     <bean id="transactionManager"         class="org.springframework.orm.hibernate3.HibernateTransactionManager">         <property name="sessionFactory" ref="sessionFactory" />     </bean> 


 

原创粉丝点击