1. 程式人生 > >關於spring核心配置檔案中的各項主要配置

關於spring核心配置檔案中的各項主要配置

  1:spring的核心配置檔案中的各種配置。      
spring的核心配置檔案的名字 叫做 applicationContext.xml,後期也可以通過配置檔案中的配置修改名稱,在web.xml中進行如下配置:

   
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
    </context-param>





2:核心配置檔案中關於dao層的配置。(1):首先準備db.properties 配置檔案,最簡單的配置如下。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456




(2):然後載入在核心配置檔案中載入資料庫檔案.

<context:property-placeholder location="classpath:resource/db.properties" />




(3):配置資料庫連線池,配置類可以用BasicDatasource,也可以用阿里巴巴的配置核心類 DruidDataSource。

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>




後期需要可以在其中新增多個屬性配置。

(4):spring和hibernate,和mybatis的整合主要是整合sessionFactory.
     和hibernate的一個整合。

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <!-- 與dataSource -->
          <property name="dataSource">
              <ref bean="dataSource"/>
         </property>
</bean>




  和mybatis的一個整合.

<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 資料庫連線池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 載入mybatis的全域性配置檔案 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>



(5):配置檔案中關於事務的配置。<

!-- 事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 資料來源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>




配置通知。

<!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 傳播行為 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>




關於切面的配置。

<!-- 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.store.service.*.*(..))" />
    </aop:config>




關於配置檔案中的service層的配置。     掃描包下面所有的service層。

<context:component-scan base-package="com.xiaoao.service"/> 




關於註解注入的配置

<mvc:annotation-driven />




(6):在進行配置的時候所需要引入的名稱空間。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">




   jar包匯入的話,可以使用maven管理,非常方便。